原题:http://www.lydsy.com/JudgeOnline/problem.php?id=3048
Description
Farmer John's N cows (1 <= N <= 100,000) are lined up in a row. Each cow is identified by an integer "breed ID" in the range 0...1,000,000,000; the breed ID of the ith cow in the lineup is B(i). Multiple cows can share the same breed ID. FJ thinks that his line of cows will look much more impressive if there is a large contiguous block of cows that all have the same breed ID. In order to create such a block, FJ chooses up to K breed IDs and removes from his lineup all the cows having those IDs. Please help FJ figure out the length of the largest consecutive block of cows with the same breed ID that he can create by doing this.
Input
* Line 1: Two space-separated integers: N and K.
* Lines 2..1+N: Line i+1 contains the breed ID B(i).
Output
* Line 1: The largest size of a contiguous block of cows with identical breed IDs that FJ can create.
Sample Input
2
7
3
7
7
3
7
5
7
INPUT DETAILS: There are 9 cows in the lineup, with breed IDs 2, 7, 3, 7, 7, 3, 7, 5, 7. FJ would like to remove up to 1 breed ID from this lineup.
Sample Output
OUTPUT DETAILS: By removing all cows with breed ID 3, the lineup reduces to 2, 7, 7, 7, 7, 5, 7. In this new lineup, there is a contiguous block of 4 cows with the same breed ID (7).
HINT
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int ans,n,k,t,now,num,l,b[200000]; struct h{ int x,y; }a[200000]; int cmp1(const h&a,const h&b) { return (a.x<b.x); } int cmp2(const h&a,const h&b) { return (a.y<b.y); } int main() { cin>>n>>k; for (int i=1;i<=n;i++) { cin>>a[i].x; a[i].y=i;//记录下原数的位置 //因为我用了排序,所以这个是为了把数离散化以后再排回来用的 } sort(a+1,a+1+n,cmp1);//排序 now=1; t=a[1].x; for (int i=1;i<=n;i++) //标号 if (a[i].x==t) a[i].x=now; else { now+=1; t=a[i].x; a[i].x=now; } sort(a+1,a+1+n,cmp2);//排回来。。。 num=0; l=1; for (int i=1;i<=n;i++) if ((b[a[i].x]==0)&&(num<=k))//如果当前这种数在队列中还一个都没有,且队列中数的种类小于等于k种,即小于k+1种 { num+=1;//数的种类加一 b[a[i].x]+=1;//队列中这种数的个数加一 ans=max(ans,b[a[i].x]);//更新答案 } else if ((b[a[i].x]==0)&&(num>k)) //当前这种数还未在队列中出现,但是队列中已经有k+1个数了 { b[a[i].x]+=1;//我们还是要把这种数加进来 b[a[l].x]-=1;//删去队首元素 while (b[a[l].x]!=0)//直到整个队列中减去了一种元素,即当前被删的队首元素在整个队列中已没有同种种类的元素了。 { l+=1; b[a[l].x]-=1; } l+=1; ans=max(ans,b[a[i].x]); } else { b[a[i].x]+=1;//把这个数加进来 ans=max(b[a[i].x],ans); } cout<<ans<<endl; return 0; }