洛谷 1540 机器翻译
【题解】
维护个队列即可。
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #define LL long long 5 #define rg register 6 #define N 200010 7 using namespace std; 8 int n,m,a[N],ans,q[N],front=1,rear; 9 inline int read(){ 10 int k=0,f=1; char c=getchar(); 11 while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar(); 12 while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar(); 13 return k*f; 14 } 15 int main(){ 16 m=read(); n=read(); 17 for(rg int i=1;i<=n;i++) a[i]=read(),q[i]=-1; 18 for(rg int i=1;i<=n;i++){ 19 bool f=0; 20 for(rg int j=front;j<=rear;j++) if(q[j]==a[i]) f=1; 21 if(!f){ 22 ans++; 23 q[++rear]=a[i]; 24 if(rear-front+1>m) front++; 25 } 26 } 27 printf("%d\n",ans); 28 return 0; 29 }