正题
给出n个数,删去k种数,使一种数连续的最长。
解题思路
用hash表储存每种数在leftleft到ii这个区间内没种数的个数,然后如果这个区间内的种数超过k+1个那么就移动指针并没次从hash中弹出一个数知道满足条件,最后统计一下答案就好了。
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#define hashmath(x) x%p
using namespace std;
const int p=600001;
int n,k,a[100001],hash[p],v[p],s,left,x,need,maxs;
int locate(int x)
{int w=hashmath(x),i=0;while (hash[(w+i)%p]!=0&&hash[(w+i)%p]!=x&&i<p) i++;return (w+i)%p;
}
int main()
{scanf("%d%d",&n,&k);k++;for (int i=1;i<=n;i++)scanf("%d",&a[i]);for (int i=1;i<=n;i++){int w=locate(a[i]);if (!v[w]) x++;//是一个新种v[w]++;hash[w]=a[i];//正常操作while(x>k){int wz=locate(a[left]);v[wz]--;if (!v[wz]){x--;//这种数已经没了hash[wz]=0;} left++;}maxs=max(maxs,v[w]);//统计答案}printf("%d",maxs);
}