luogu1540 [NOIp2011]机器翻译 (队列)
1 #include<bits/stdc++.h> 2 #define pa pair<int,int> 3 #define CLR(a,x) memset(a,x,sizeof(a)) 4 using namespace std; 5 typedef long long ll; 6 const int maxn=1010,maxm=110; 7 8 inline ll rd(){ 9 ll x=0;char c=getchar();int neg=1; 10 while(c<'0'||c>'9'){if(c=='-') neg=-1;c=getchar();} 11 while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); 12 return x*neg; 13 } 14 15 queue<int> q; 16 int N,M; 17 bool ins[maxn]; 18 19 int main(){ 20 //freopen("","r",stdin); 21 int i,j,k,ans=0; 22 M=rd(),N=rd(); 23 for(i=1;i<=N;i++){ 24 int x=rd(); 25 if(!ins[x]){ 26 ans++; 27 if(q.size()>=M){ 28 ins[q.front()]=0;q.pop(); 29 } 30 ins[x]=1;q.push(x); 31 } 32 } 33 printf("%d\n",ans); 34 return 0; 35 }