AcWing 1980. 奶牛排队
原题链接
困难
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1e5+1e4;
map<ll,ll>Map,m;
ll t,n,k,cnt;
ll a[N],last[N],ha[N];
void Add(ll x)
{
ha[m[x]]++;
if(ha[m[x]]==1)cnt++;
}
void Del(ll x)
{
ha[m[x]]--;
if(ha[m[x]]==0)cnt--;
}
signed main()
{
cin>>n>>k;
for(ll i=1;i<=n;i++)
{
cin>>a[i];
if(m[a[i]]==0)m[a[i]]=++t;
last[i]=Map[a[i]];
Map[a[i]]=i;
}
ll l=1;
ll ans=0;
for(int i=1;i<=n;i++)
{
Add(a[i]);
while(cnt-1>k)
{
Del(a[l]);
l++;
}
ans=max(ans,ha[m[a[i]]]);
}
cout<<ans;
return 0;
}