HDU - 6602 Longest 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

              

#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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值