让人很喜欢的一道水题

P1540 [NOIP2010 提高组] 机器翻译 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

从学计组学到操作系统,学了很多算法,对于这种偏硬件的科目,能够在这道题上体现所学的,真的很惊喜了!

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int M,N,ans=0;
bool vis[1010];
queue<int> q;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>M>>N;
    for(int i=1;i<=N;i++){
        int x;
        cin>>x;
        if(vis[x]) continue;
        else{
            if(q.size()>=M){
                vis[q.front()]=false;
                q.pop();
            }
            q.push(x);
            vis[x]=true;
            ans++;
        }
    }
    cout<<ans<<endl;
    
    return 0;
}

 

posted @ 2024-02-19 22:03  ACCbulb  阅读(10)  评论(0)    收藏  举报