赞
踩
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
- #include<cstdio>
- #include<iostream>
- #include<algorithm>
- #include<vector>
- using namespace std;
- #define ll long long
- #define lson l,m,rt<<1
- #define rson m+1,r,rt<<1|1
- int n, c, k;
- const int inf = 0x3f3f3f3f;
- const int maxn = 100005;
- int t[maxn * 4], la[maxn * 4];
- int d[maxn], num[maxn];
- vector<int>pos[maxn];
- void build(int l, int r, int rt) {
- t[rt] = la[rt] = 0;
- if (l == r)return;
- int m = (l + r) >> 1;
- build(lson); build(rson);
- }
- void pushup(int rt) {
- t[rt] = max(t[rt << 1], t[rt << 1 | 1]);
- }
- void pushdown(int rt) {
- if (la[rt] == 0) return;
- t[rt << 1] += la[rt]; la[rt << 1] += la[rt];
- t[rt << 1 | 1] += la[rt]; la[rt << 1 | 1] += la[rt];
- la[rt] = 0;
- }
- void updata(int l, int r, int rt, int L, int R,int x) {
- if (L <= l && r <= R) {
- la[rt] += x;
- t[rt] += x;
- return;
- }
- pushdown(rt);
- int m = (l + r) >> 1;
- if (L <= m)updata(lson, L, R, x);
- if (R > m)updata(rson, L, R, x);
- pushup(rt);
- return;
- }
- int query(int l, int r, int rt) {
- if (l == r) {
- if (t[rt] == c)return l;
- return -1;
- }
- int m = (l + r) >> 1;
- pushdown(rt);
- if (t[rt << 1] == c)
- return query(lson);
- if (t[rt << 1 | 1] == c)
- return query(rson);
- return -1;
- }
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(0); cout.tie(0);
- while (cin >> n >> c >> k) {
- for (int i = 0; i <= c; i++) {
- pos[i].clear(); pos[i].push_back(0);
- }
- for (int i = 1; i <= n; i++) {
- cin >> d[i]; num[i] = 0;
- pos[d[i]].push_back(i);
- }
- if (k == 1) {
- cout << n << "\n"; continue;
- }
- build(1, n, 1);
- int ans = 0;
- for (int i = 1; i <= n; i++) {
- int u = d[i];
- int p = ++num[u];
- updata(1, n, 1, i, i, c - 1);
- if (pos[u][p - 1] + 1 <= pos[u][p] - 1)
- updata(1, n, 1, pos[u][p - 1] + 1, pos[u][p] - 1, -1);
- if (p >= k)
- updata(1, n, 1, pos[u][p - k] + 1, pos[u][p - k + 1], 1);
- int res = query(1, n, 1);
- if (res != -1)
- ans = max(ans, i - res + 1);
- }
- cout << ans << "\n";
- }
- return 0;
- }
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。