图书馆

  • Description

临近期末,去图书馆自习的人越来越多。图书馆一共有N张桌子,依次编号为1,2……,N;每张桌子都配有6把椅子。结伴去自习的GGMM们有个习惯,优先选择已坐人数最少的桌子,如果不只一种选择,则选择编号最小的桌子。同时,他们不会分开选择不同的桌子。

  • Input

多组测试数据,每组数据第1行输入两个正整数N,Q(1≤N,Q≤50000)。接下来输入Q行,依次描述Q组事件。事件的输入格式如下:

① I a:有a位结伴的GGMM进入图书馆自习;

② D a b:编号为b的桌子上有a位GGMM离开;

③ C b:查询编号为b的桌子的空位数;

(1≤a≤6,1≤b≤N,第一组事件发生之前所有的座位都是空着的)输入直至文件结尾。

  • Output

每组数据输出Q行。

对于事件①:如果成功地找到了座位,则输出桌子的编号,如果没能找到则输出“failed”;

对于事件②:如果编号为b的桌子当前坐着的人数小于a则输出“error”,并过滤这组事件,否则正常处理并输出“success”;

对于事件③:按要求输出空位数。(具体格式见样例)

  • Sample Input

1 5
I 5
I 2
D 6 1
C 1
D 3 1
2 5
I 1
I 1
I 3
I 3
I 3

  • Sample Output

1
failed
error
1
success
1
2
1
2
failed

#include<queue>
#include<cstring>
#include<iostream>
using namespace std;

struct Table
{
    int tag,person;
    bool operator < (const Table &a) const
    {
        if(a.person==person) return a.tag<tag;
        else return a.person<person;
    }
};

int N,Q,P[50050];

priority_queue<Table> pq;

void Pre()
{
    memset(P,0,sizeof(int)*(N+5));

    while(pq.empty()!=true)
    {
        pq.pop();
    }

    for(int i=1;i<=N;i++)
    {
        Table tmp;
        tmp.tag=i,tmp.person=0;
        pq.push(tmp);
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    while(cin>>N>>Q)
    {
        Pre();
        int a,b;
        char s[2];
        Table tmp;

        for(int i=0;i<Q;i++)
        {
            scanf("%s",s);
            if(s[0]=='C')
            {
                scanf("%d",&b);
                printf("%d\n",6-P[b]);
                continue;
            }
            if(s[0]=='D')
            {
                scanf("%d%d",&a,&b);
                if(P[b]>=a)
                {
                    P[b]-=a;
                    tmp.tag=b, tmp.person=P[b];
                    pq.push(tmp);
                    printf("success\n");
                }
                else printf("error\n");
                continue;
            }
            if(s[0]=='I')
            {
                scanf("%d",&a);
                while(pq.top().person!=P[pq.top().tag])
                {
                    pq.pop();
                }
                if(pq.top().person+a<=6)
                {
                    printf("%d\n",pq.top().tag);
                    P[pq.top().tag]+=a;
                    tmp.tag=pq.top().tag, tmp.person=P[pq.top().tag];
                    pq.pop(), pq.push(tmp);
                }
                else printf("failed\n");
                continue;
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值