洛谷P2776 [SDOI2007]小组队列 链表 + 模拟

本文详细介绍了如何使用C++实现一种特殊的队列操作,包括元素的入队和出队,特别关注了队列被弹空的情况处理。通过具体代码示例,展示了如何有效地管理和更新队列中的元素类型及状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有些细节需要注意:
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值