当前位置:   article > 正文

HDU - 6602 Longest Subarray (思维+线段树)_hdu longet subarray

hdu longet subarray

You are given two integers C,KC,K and an array of NN integers a1,a2,...,aNa1,a2,...,aN. It is guaranteed that the value of aiai is between 11 to CC. 

We define that a continuous subsequence al,al+1,...,ar(l≤r)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∀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 KKtimes. 

You should find the longest good subarray and output its length. Or you should print 00 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)N,C,K(N,C,K≤105). 

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

We guarantee that the sum of NNs, the sum of CCs and the sum of KKs in all test cases are all no larger than 5×1055×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

              

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<vector>
  5. using namespace std;
  6. #define ll long long
  7. #define lson l,m,rt<<1
  8. #define rson m+1,r,rt<<1|1
  9. int n, c, k;
  10. const int inf = 0x3f3f3f3f;
  11. const int maxn = 100005;
  12. int t[maxn * 4], la[maxn * 4];
  13. int d[maxn], num[maxn];
  14. vector<int>pos[maxn];
  15. void build(int l, int r, int rt) {
  16. t[rt] = la[rt] = 0;
  17. if (l == r)return;
  18. int m = (l + r) >> 1;
  19. build(lson); build(rson);
  20. }
  21. void pushup(int rt) {
  22. t[rt] = max(t[rt << 1], t[rt << 1 | 1]);
  23. }
  24. void pushdown(int rt) {
  25. if (la[rt] == 0) return;
  26. t[rt << 1] += la[rt]; la[rt << 1] += la[rt];
  27. t[rt << 1 | 1] += la[rt]; la[rt << 1 | 1] += la[rt];
  28. la[rt] = 0;
  29. }
  30. void updata(int l, int r, int rt, int L, int R,int x) {
  31. if (L <= l && r <= R) {
  32. la[rt] += x;
  33. t[rt] += x;
  34. return;
  35. }
  36. pushdown(rt);
  37. int m = (l + r) >> 1;
  38. if (L <= m)updata(lson, L, R, x);
  39. if (R > m)updata(rson, L, R, x);
  40. pushup(rt);
  41. return;
  42. }
  43. int query(int l, int r, int rt) {
  44. if (l == r) {
  45. if (t[rt] == c)return l;
  46. return -1;
  47. }
  48. int m = (l + r) >> 1;
  49. pushdown(rt);
  50. if (t[rt << 1] == c)
  51. return query(lson);
  52. if (t[rt << 1 | 1] == c)
  53. return query(rson);
  54. return -1;
  55. }
  56. int main() {
  57. ios::sync_with_stdio(0);
  58. cin.tie(0); cout.tie(0);
  59. while (cin >> n >> c >> k) {
  60. for (int i = 0; i <= c; i++) {
  61. pos[i].clear(); pos[i].push_back(0);
  62. }
  63. for (int i = 1; i <= n; i++) {
  64. cin >> d[i]; num[i] = 0;
  65. pos[d[i]].push_back(i);
  66. }
  67. if (k == 1) {
  68. cout << n << "\n"; continue;
  69. }
  70. build(1, n, 1);
  71. int ans = 0;
  72. for (int i = 1; i <= n; i++) {
  73. int u = d[i];
  74. int p = ++num[u];
  75. updata(1, n, 1, i, i, c - 1);
  76. if (pos[u][p - 1] + 1 <= pos[u][p] - 1)
  77. updata(1, n, 1, pos[u][p - 1] + 1, pos[u][p] - 1, -1);
  78. if (p >= k)
  79. updata(1, n, 1, pos[u][p - k] + 1, pos[u][p - k + 1], 1);
  80. int res = query(1, n, 1);
  81. if (res != -1)
  82. ans = max(ans, i - res + 1);
  83. }
  84. cout << ans << "\n";
  85. }
  86. return 0;
  87. }

 

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

闽ICP备14008679号