其他分享
首页 > 其他分享> > [USACO2013JAN]Cow Lineup G

[USACO2013JAN]Cow Lineup G

作者:互联网

题意:一串数字,选出k种血统的奶牛,并把他们全部从队列中赶走,使其连续段最大(即一段中数字的种类<=k+1)

吐槽:想找个双指针的题学习;还用到离散化,刚好复习一下昨天学到的map

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
char getc(){
    static char buf[1<<14],*p1=buf,*p2=buf;
    return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<14,stdin),p1==p2)?EOF:*p1++;
}
int read(){
   int s=0,w=1;  
   char ch=getchar();  
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}  
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();  
   return s*w;  
}
#define ll long long
#define ull unsigned long long
const int inf=0x7f7f7f7f;
const int N=100005;

map<int, int> p;
int n, k, js=0, tot=0, ans=0;
int a[N], cnt[N];

int main(){
//    freopen(".in","r",stdin);
//    freopen(".out","w",stdout);
//    printf("%.3lf M\n",(double)sizeof(/*szm*/)/(1<<20));
//    clock_t start_time=clock();
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; i++){
        a[i]=read();
        if(!p[a[i]]) p[a[i]] = ++js;
    }
    
    for(int l=1,r=1; r<=n; r++){
        if(cnt[p[a[r]]]==0) tot++;
        cnt[p[a[r]]]++;
        while(tot==k+2){
            cnt[p[a[l]]]--;
            if(cnt[p[a[l]]]==0) tot--;
            l++;
        }
        ans = max(ans, cnt[p[a[r]]]);
    }
    printf("%d\n",ans);
    
//    clock_t end_time=clock();
//cout<<"Running time is:"<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;
    fclose(stdin);fclose(stdout);
    return 0;
}

 

标签:char,ch,Cow,int,Lineup,USACO2013JAN,freopen,time,include
来源: https://www.cnblogs.com/EvfX/p/15937875.html