hoj1456 Team Queue 优先队列

/*

题目:
    模拟排队的实现,但是若是有同伙的话,这些家伙就会插队,排到他们的同伙的后面,要不就乖乖地排到队伍的后面。现在给出朋友关系以及进队出队的顺序,问你当出队时是谁在出队

分析:
    利用优先队列,重载小于号,使得优先级最大的先出队,优先级的定义如下:当前面有同伙时,按照前面的同伙的优先级来插入,若没有的话,就按照现在的优先插入。当优先级相同时,按照先进队的优先级越大就越大。


*/
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>

using namespace std;

const int X = 100005;
const int maxn = 999999+1;

int ha[maxn];
int use[maxn];
int map[maxn];

struct node
{
    int id,p;
    int cnt;
    int qq;
    friend bool operator < (node a,node b)
    {
        return a.p>b.p||(a.p==b.p&&a.qq>b.qq);
    }
}p[X];

int main()
{
    freopen("sum.in","r",stdin);
    int x,n;
    node aa;
    char s[21];
    int ncase = 0;
    while(cin>>n,n)
    {
        for(int i=0;i<maxn;i++)
            use[i] = -1;
        memset(map,0,sizeof(map));
        printf("Scenario #%d\n",++ncase);
        for(int i=0;i<=999999;i++)
            ha[i] = -1;
        int cnt = 0;
        priority_queue<node> q;
        while(!q.empty())
            q.pop();
        for(int i=0;i<n;i++)
        {
            scanf("%d",&x);
            while(x--)
            {
                scanf("%d",&p[cnt].id);
                ha[p[cnt].id] = cnt;
                p[cnt].cnt = i;
                cnt++;
            }
        }
        int ret = 0;
        int y;
        int qqq = 0;
        while(scanf("%s",s),s[0]!='S')
        {
            if(s[0]=='E')
            {
                scanf("%d",&x);
                x = ha[x];
                y = p[x].cnt;
                p[x].qq = qqq++;
                if(use[y]!=-1&&map[y])
                    p[x].p = use[y];
                else
                {
                    p[x].p = ret;
                    use[y] = ret;
                    ret++;
                }
                map[y]++;
                q.push(p[x]);
            }
            else
            {
                aa = q.top();
                q.pop();
                y = aa.cnt;
                map[y]--;
                printf("%d\n",aa.id);
            }
        }
        printf("\n");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/yejinru/p/2608081.html