赞
踩
You are given two integers C , K C,K C,K and an array of N N N integers a 1 , a 2 , . . . , a N a_1,a_2,...,a_N a1,a2,...,aN. It is guaranteed that the value of ai is between 1 1 1 to C C C.
We define that a continuous subsequence a l , a l + 1 , . . . , a r ( l ≤ r ) a_l,a_{l+1},...,a_r(l\leq 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 = l r [ a i = x ] = 0 o r ∑ i = l r [ a i = x ] ≥ K \forall x\in[1,C],\sum_{i=l}^{r}{[ai=x]=0}\ or\sum_{i=l}^{r}{[ai=x]}\geq K ∀x∈[1,C],∑i=lr[ai=x]=0 or∑i=lr[ai=x]≥K
It implies that if a number appears in the subarray, it will appear no less than K K K times.
You should find the longest good subarray and output its length. Or you should print 0 0 0 if you cannot find any.
There are multiple test cases.
Each case starts with a line containing three positive integers N , C , K ( N , C , K ≤ 1 0 5 ) N,C,K(N,C,K\leq 10^5) N,C,K(N,C,K≤105).
The second line contains N integer a 1 , a 2 , . . . , a N ( 1 ≤ a i ≤ C ) a_1,a_2,...,a_N(1\leq a_i \leq C) a1,a2,...,aN(1≤ai≤C).
We guarantee that the sum of N s Ns Ns, the sum of C s Cs Cs and the sum of K s Ks Ks in all test cases are all no larger than 5 × 1 0 5 5\times 10^5 5×105.
For each test case, output one line containing an integer denoting the length of the longest good subarray.
7 4 2
2 1 4 1 4 3 2
4
2019 Multi-University Training Contest 2
#include<bits/stdc++.h> using namespace std; const int maxn=1e5+10; int n,m,k,a[maxn],mark[maxn<<2],val[maxn<<2],maxx[maxn<<2],pos[maxn<<2]; deque<int> que[maxn]; //que[i]维护数i的最多k个最新位置信息 void init() { for(int i=1;i<=m;i++) que[i].clear(); for(int i=1;i<=m;i++) que[i].push_back(0);//插入0,使后面代码整洁 } void pushup(int id) { if(maxx[id<<1]>=maxx[id<<1|1]) maxx[id]=maxx[id<<1],pos[id]=pos[id<<1]; else maxx[id]=maxx[id<<1|1],pos[id]=pos[id<<1|1]; } void down(int id) { maxx[id<<1]+=mark[id]; maxx[id<<1|1]+=mark[id]; mark[id<<1]+=mark[id]; mark[id<<1|1]+=mark[id]; mark[id]=0; } void build(int id,int l,int r) { int mid=(l+r)>>1;mark[id]=0,val[id]=0;maxx[id]=0;pos[id]=l; if(l==r) return; build(id<<1,l,mid);build(id<<1|1,mid+1,r); } void update(int id,int L,int R,int l,int r,int add) { if(l<=L&&R<=r) { mark[id]+=add;maxx[id]+=add; return; } if(mark[id]) down(id); int mid=(L+R)>>1; if(l<=mid) update(id<<1,L,mid,l,r,add); if(r>mid) update(id<<1|1,mid+1,R,l,r,add); pushup(id); } pair<int,int> query(int id,int L,int R,int l,int r) { pair<int,int> resl=make_pair(-1,-1),resr=make_pair(-1,-1); if(l<=L&&R<=r) return make_pair(maxx[id],pos[id]); if(mark[id]) down(id); int mid=(L+R)>>1; if(l<=mid) resl=query(id<<1,L,mid,l,r); if(r>mid) resr=query(id<<1|1,mid+1,R,l,r); if(resl.first>=resr.first) return resl; return resr; } int get(int l,int r) { auto res=query(1,1,n,l,r); if(res.first!=m) return -1; return res.second; } int main() { while(~scanf("%d %d %d",&n,&m,&k)) { init();int ans=0; for(int i=1;i<=n;i++) scanf("%d",&a[i]); if(k==1) {printf("%d\n",n);continue;} build(1,1,n); for(int i=1;i<=n;i++) { int last_pos=que[a[i]].back(); if(k!=1&&last_pos+2<=i) update(1,1,n,last_pos+1,i-1,-1); if(que[a[i]].size()==k) { int fir_pos=que[a[i]].front();que[a[i]].pop_front(); int nxt=que[a[i]].front(); update(1,1,n,fir_pos+1,nxt,1); } que[a[i]].push_back(i); update(1,1,n,i,i,m-1); int q=get(1,i); if(q!=-1) ans=max(ans,i-q+1); } printf("%d\n",ans); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。