欢迎来到SFWR的博客

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;
}

 

posted @ 2019-08-30 21:49  SFWR  Views(117)  Comments(0Edit  收藏  举报