[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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现