使用集合避免在缓存中进行n次查询,缩短至logn次

#include <iostream>
#include<queue>
#include<set>
using namespace std;

queue<int> fr;
set<int> fi;
int main() {
    int m ,n;cin >> m >> n;
    int ai;
    int times =0;
    while(n--){
        cin >> ai;
        if (fi.find(ai) == fi.end()){ // 缓存找不到
            times++;
            fr.push(ai);fi.insert(ai);
            if(fi.size() == m+1){ // 缓存超量
                fi.erase(fr.front());
                fr.pop();
            }
        }
    }cout << times;
}