有些细节需要注意:
1.编号和元素种类都从0开始标号。
2.需要特判一下队列被弹空的情况。
Code:
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1000000 + 4;
int typ[maxn], tail[maxn];
int head[maxn], nex[maxn], cur[maxn], cnt, back;
int main()
{
// freopen("in.txt","r",stdin);
int n,m, ops, pre = 1;
scanf("%d%d",&n,&m);
for(int i = 1;i <= n; ++i)
{
scanf("%d",&typ[i]);
typ[i] += 1;
}
scanf("%d",&ops);
while(ops--)
{
char A[10];
scanf("%s",A);
if(A[1] == 'u')
{
int u,k;
scanf("%d",&k);
k += 1;
u = typ[k];
if(!tail[u] || tail[u] == back)
{
nex[back] = ++ cnt;
cur[cnt] = k;
tail[u] = back = cnt;
}
else
{
nex[++cnt] = nex[tail[u]], cur[cnt] = k;
nex[tail[u]] = cnt, tail[u] = cnt;
}
}
else
{
printf("%d\n",cur[pre] - 1);
if(typ[cur[nex[pre]]] != typ[cur[pre]] ) tail[typ[cur[pre]]] = 0;
pre = nex[pre] ? nex[pre] : cnt + 1;
}
}
return 0;
}
本文详细介绍了如何使用C++实现一种特殊的队列操作,包括元素的入队和出队,特别关注了队列被弹空的情况处理。通过具体代码示例,展示了如何有效地管理和更新队列中的元素类型及状态。
4058

被折叠的 条评论
为什么被折叠?



