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

有些细节需要注意:
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;
}
posted @ 2018-09-06 18:30  EM-LGH  阅读(132)  评论(0编辑  收藏  举报