当前位置:   article > 正文

HDU6602 Longest Subarray(思维+线段树区间修改维护最值)_it implies that if a number appears in the subarra

it implies that if a number appears in the subarray, it will appear no less

Longest Subarray

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2206    Accepted Submission(s): 790


 

Problem Description

You are given two integers C,K and an array of N integers a1,a2,...,aN . It is guaranteed that the value of ai is between 1 to C .

We define that a continuous subsequence al,al+1,...,ar(l≤r) of array a is a good subarray if and only if the following condition is met:
 

∀x∈[1,C],∑i=lr[ai=x]=0or∑i=lr[ai=x]≥K



It implies that if a number appears in the subarray, it will appear no less than K times.

You should find the longest good subarray and output its length. Or you should print 0 if you cannot find any.

 

 

Input

There are multiple test cases.

Each case starts with a line containing three positive integers N,C,K(N,C,K≤105) .

The second line contains N integer a1,a2,...,aN(1≤ai≤C) .

We guarantee that the sum of N s, the sum of C s and the sum of K s in all test cases are all no larger than 5×105 .

 

 

Output

For each test case, output one line containing an integer denoting the length of the longest good subarray.

 

 

Sample Input

 

7 4 2 2 1 4 1 4 3 2

 

 

Sample Output

 

4

 

 

Source

2019 Multi-University Training Contest 2

 

 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6742 6741 6740 6739 6738 


OJ题号

HDU - 6602

简单题意

题意:求最长的连续子序列,满足序列中每个数至少出现 k 次或不出现。

正解思路

我们枚举右端点R,寻找满足题意的最远的左端点,假设我们当前要寻找k=3,

首先对于单个点来说,肯定有c-1种数满足答案,所以每个点来说初值为c-1.

你可能会疑惑62也是不满足为什么不加入,但我们是从前面递推过来的这个62会在它的2加上,满足的也同理。

 

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stack>
  5. #include <queue>
  6. #include <map>
  7. #include <set>
  8. #include <vector>
  9. #include <math.h>
  10. #include <bitset>
  11. #include <algorithm>
  12. #include <climits>
  13. using namespace std;
  14. #define lson i<<1
  15. #define rson i<<1|1
  16. #define LS l,mid,lson
  17. #define RS mid+1,r,rson
  18. #define LL long long
  19. #define N 1000005
  20. #define MOD 1000000007
  21. #define INF 0x3f3f3f3f
  22. #define EXP 1e-8
  23. #define lowbit(x) (x&-x)
  24. int ans_sum,ans_maxx,ans_minn;
  25. struct node
  26. {
  27. int l,r;
  28. int maxx;
  29. int add;
  30. } tree[N<<2];
  31. void pushdown(int i)//标记下传
  32. {
  33. tree[lson].add+=tree[i].add;///给左子节点打上延迟标记
  34. tree[rson].add+=tree[i].add;///给右子节点打上延迟标记
  35. //tree[lson].minn+=tree[i].add;
  36. //tree[rson].minn+=tree[i].add;
  37. tree[lson].maxx+=tree[i].add;
  38. tree[rson].maxx+=tree[i].add;
  39. //tree[lson].sum+=tree[i].add*(tree[lson].r-tree[lson].l+1); ///更新左子节点消息
  40. //tree[rson].sum+=tree[i].add*(tree[rson].r-tree[rson].l+1); ///更新右子节点消息
  41. tree[i].add = 0; ///清除标记
  42. }
  43. void pushup(int i)
  44. {
  45. // tree[i].sum=tree[lson].sum+tree[rson].sum;
  46. tree[i].maxx=max(tree[lson].maxx,tree[rson].maxx);
  47. //tree[i].minn=min(tree[lson].minn,tree[rson].minn);
  48. }
  49. //建立线段树
  50. void build(int l,int r,int i)
  51. {
  52. tree[i].l = l;
  53. tree[i].r = r;
  54. tree[i].add = 0;
  55. //tree[i].sett = -1;
  56. if(l == r)
  57. {
  58. //tree[i].sum = 0;
  59. tree[i].maxx = 0;
  60. //tree[i].minn = 0;
  61. return;
  62. }
  63. int mid = (l+r)>>1;
  64. build(LS);
  65. build(RS);
  66. pushup(i);
  67. }
  68. //tree[l,r]+=val
  69. void add_data(int l,int r,int i,int val)
  70. {
  71. if(tree[i].l>=l&&tree[i].r<=r)
  72. {
  73. tree[i].maxx += val;
  74. tree[i].add += val;
  75. return;
  76. }
  77. pushdown(i);//标记下传
  78. int mid = (tree[i].l+tree[i].r)>>1;
  79. if(l<=mid)
  80. add_data(l,r,lson,val);
  81. if(r>mid)
  82. add_data(l,r,rson,val);
  83. pushup(i);
  84. }
  85. void query(int l,int r,int i)
  86. {
  87. if(l <= tree[i].l && tree[i].r <= r)
  88. {
  89. //ans_sum += tree[i].sum;
  90. ans_maxx = max(ans_maxx,tree[i].maxx);
  91. //ans_minn = min(ans_minn,tree[i].minn);
  92. return ;
  93. }
  94. pushdown(i);
  95. int mid = (tree[i].l+tree[i].r)>>1;
  96. if(l<=mid)
  97. query(l,r,lson);
  98. if(r>mid)
  99. query(l,r,rson);
  100. pushup(i);
  101. }
  102. int query_pos(int rt,int val)
  103. {
  104. int l=tree[rt].l;
  105. int r=tree[rt].r;
  106. if(l==r)
  107. {
  108. return l;
  109. }
  110. pushdown(rt);
  111. if(tree[rt<<1].maxx==val)
  112. return query_pos(rt<<1,val);
  113. else if(tree[rt<<1|1].maxx==val)
  114. return query_pos(rt<<1|1,val);
  115. else
  116. return -1;
  117. pushup(rt);
  118. }
  119. int n,c,k;
  120. vector<int>pos[N];
  121. int a[N],num[N];
  122. int main()
  123. {
  124. while(~scanf("%d%d%d",&n,&c,&k))
  125. {
  126. for(int i=0; i<=c; i++)
  127. {
  128. num[i]=0;
  129. pos[i].clear();
  130. pos[i].push_back(0);
  131. }
  132. for(int i=1; i<=n; i++)
  133. {
  134. scanf("%d",&a[i]);
  135. pos[a[i]].push_back(i);
  136. }
  137. if(k==1)
  138. {
  139. printf("%d\n",n);
  140. continue;
  141. }
  142. build(1,n,1);
  143. LL ans=0;
  144. for(int i=1; i<=n; i++)
  145. {
  146. add_data(i,i,1,c-1);
  147. num[a[i]]++;
  148. if(pos[a[i]][num[a[i]]]-1>=pos[a[i]][num[a[i]]-1]+1)
  149. {
  150. add_data(pos[a[i]][num[a[i]]-1]+1,pos[a[i]][num[a[i]]]-1,1,-1);
  151. }
  152. if(num[a[i]]>=k)
  153. {
  154. add_data(pos[a[i]][num[a[i]]-k]+1,pos[a[i]][num[a[i]]-k+1],1,1);
  155. }
  156. int templ=query_pos(1,c);
  157. if(templ!=-1)
  158. ans=max(ans,(LL)i-templ+1);
  159. }
  160. printf("%lld\n",ans);
  161. }
  162. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/180276
推荐阅读
相关标签
  

闽ICP备14008679号