很简单的一道题,清理缓存是将最开始存的删除,这跟队列的先进先出的特点很像。然后用一个check数组判断这个单词在不在缓存里面

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int n, m, k, T;
queue<int> qu;
bool check[1010] = {};
signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);//关闭输入输出流
    cin >> m >> n;
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        int x;
        cin >> x;
        if (check[x]) {}
        else {
            ans++;
            if (qu.size() == m) {
                check[qu.front()] = false;
                qu.pop();
                check[x] = true;
                qu.push(x);
            } else {
                qu.push(x);
                check[x] = true;
            }
        }
    }
    cout << ans;
    return 0;
}