Loading

P1540 机器翻译

#include<bits/stdc++.h>
using namespace std;
int M,N;
int a[1005],book[1005],ans;
queue<int> q;
int main()
{
    cin>>M>>N;
    for(int i=1;i<=N;i++)
        cin>>a[i];
    for(int i=1;i<=N;i++)
    {
        if(book[a[i]]==1) continue;//如果内存中有a[i],跳过
        else
        {
            if(q.size()<M)//如果内存未满 
            {
                q.push(a[i]);//内存读入
                ans++;//读取次数加一
                book[a[i]]=1; 
            }
            else
            {
             book[q.front()]=0;
             q.pop();
             q.push(a[i]);
             book[a[i]]=1;
             ans++;
             
             } 
        }
    }
    cout<<ans<<endl;
}

 

posted @ 2018-08-15 18:59  AntiO2  阅读(254)  评论(0)    收藏  举报