P3069 [USACO13JAN]牛的阵容Cow Lineup


离散化加尺取


#include<bits/stdc++.h>
using namespace std;
map<int,int>mp;
int n,num[100010],k,now[100010],ll=0,cow[100010],l=1,r=0,typ=0,ans=-1;
int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>num[i];
        if(!mp[num[i]])mp[num[i]]=++ll;
        now[i]=mp[num[i]];
    }
    while(r<=n)
    {    r++;
        if(!cow[now[r]])typ++;
        cow[now[r]]++;
        while(typ>k+1)
        {
            cow[now[l]]--;
            if(!cow[now[l]])typ--;
            l++;
        }
        ans=max(ans,cow[now[r]]);
    }
    cout<<ans;
}
原文地址:https://www.cnblogs.com/SFWR-YOU/p/11437297.html