题目:
题解:
哎呀看起来高大上的。。。。其实很简单啊,这个莫队也是伪
如果ta队列里的元素>k+1(就是不能靠删减直接取里面的最大值了)就head++;
这里认真介绍一下:
lower bound//大于等于a[i]的第一个位置
Returns an iterator pointing to the first element in the range [first,last) which does not compare less than valupper bound//大于a[i]的第一个位置
Returns an iterator pointing to the first element in the range [first,last) which compares greater than val.
代码:
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#define N 100005
using namespace std;
int a[N],b[N],c[N],q[N];
int main()
{
int n,k,i;
scanf("%d%d",&n,&k);
for (i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i];
sort(b+1,b+n+1);
int cnt=unique(b+1,b+n+1)-b-1;//多少种不同的数字
for (int i=1;i<=n;i++)
a[i]=lower_bound(b+1,b+n+1,a[i])-b;//大于等于a[i]的第一个位置
//离散
int head=1,tail=0,kind=0,maxx=0;
for (i=1;i<=n;i++)
{
if (c[a[i]]==0) kind++;
while (kind>k+1)
{
c[q[head]]--;
if (!c[q[head]]) kind--;
head++;
}
c[a[i]]++;maxx=max(maxx,c[a[i]]);
q[++tail]=a[i];
}
printf("%d",maxx);
}