标签:
用队列维护一下即可
#include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; const int maxn=500000+10; int n,k; int a[maxn]; int tot[1000000+10]; struct Node { int id; int val; } node[maxn]; queue<Node>Q; int main() { scanf("%d%d",&n,&k); for(int i=1; i<=n; i++) { scanf("%d",&a[i]); node[i].id=i; node[i].val=a[i]; } memset(tot,0,sizeof tot); int ans=0,ansl,ansr; int now=0; for(int i=1; i<=n; i++) { Q.push(node[i]); if(tot[node[i].val]==0) now++; tot[node[i].val]++; if(now<=k) { Node head=Q.front(); if(node[i].id-head.id+1>ans) { ans=node[i].id-head.id+1; ansl=head.id; ansr=node[i].id; } } else if(now>k) { while(1) { Node head=Q.front(); if(tot[head.val]==1) now--; tot[head.val]--; Q.pop(); if(now==k) break; } Node head=Q.front(); if(node[i].id-head.id+1>ans) { ans=node[i].id-head.id+1; ansl=head.id; ansr=node[i].id; } } } printf("%d %d\n",ansl,ansr); return 0; }
CodeForces 616D Longest k-Good Segment
标签:
原文地址:http://www.cnblogs.com/zufezzt/p/5130246.html