题目链接:点击这里
队列模拟题,但是STL的queue没有查找函数,便用hash数组标记,AC感谢数据量小:
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;
const int MOD = 10000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = 1010;
int hash[maxn];
int main()
{
queue<int> Q;
int m,n,temp;
scanf("%d%d",&m,&n);
int cnt = 0;
for(int i=0;i<n;i++)
{
scanf("%d",&temp);
if(hash[temp]) //如果temp在内存中,什么也不操作
continue;
//如果temp不在内存中,便去外存查找,分情况入队列
cnt++;
if(Q.size()<m) //内存未满,标记后直接入队列
{
hash[temp] = 1;
Q.push(temp);
}
else if(Q.size()==m) //内存已满,移除队首元素,入队列
{
hash[Q.front()] = 0;
Q.pop();
hash[temp] = 1;
Q.push(temp);
}
}
printf("%d\n",cnt);
return 0;
}
来源:https://blog.csdn.net/qq_42815188/article/details/99415883