hdu5437

来源:互联网 发布:英雄联盟t恤淘宝 编辑:程序博客网 时间:2024/06/06 04:58

题目链接:hdu5437


题目大意:主人在城堡举行生日party,但是城堡的门不会一直打开,而是在特定时间允许特定人数的人进入城堡。现在已知每个客人的名字,以及所送礼物的价值。现在有m行表示主人开门的时间,每行有两个数a,b,主人会在第a个客人来之后打开门,并且允许b个客人进入城堡。当外面的客人人数大于b时,优先允许礼物价值高的,价值相等优先允许先到的客人先进入城堡。最后当所有客人来了之后,主人会最后一次开门,让外面的客人全部进入城堡。现在给出q个询问,问第ai个客人的名字是什么?


题目分析:直接用优先队列,重载其排序规则,但是这里要注意的是主人开门的时间并不是递增的。(ps:参考大神队友的代码点击打开链接)


代码:

#include<queue>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define maxn 150005struct node{    char str[205];    int w,t;    bool operator < (const node& x) const {        if(w==x.w) return t>x.t;        return w<x.w;    }}a[maxn],ans[maxn];struct time{    int t,people;    bool operator < (const time& x) const {        return t<x.t;    }}door[maxn];int query[maxn];int main(){    int k,m,q,T;    //freopen("in.txt","r",stdin);    scanf("%d",&T);    while(T--)    {        scanf("%d%d%d",&k,&m,&q);        for(int i=0;i<k;i++)            scanf("%s%d",a[i].str,&a[i].w),a[i].t=i+1;        for(int i=0;i<m;i++)            scanf("%d%d",&door[i].t,&door[i].people);        sort(door,door+m);        for(int i=0;i<q;i++)            scanf("%d",&query[i]);        int now=0,pos=0;        door[m].t=0x3f3f3f;        priority_queue<node>pp;        for(int i=0;i<k;i++){            if(a[i].t<=door[now].t)                pp.push(a[i]);            else{                while((door[now].people--)&&(!pp.empty()))                    ans[pos++]=pp.top(),pp.pop();                pp.push(a[i]);                now++;            }        }        while(!pp.empty()) ans[pos++]=pp.top(),pp.pop();        for(int i=0;i<q;i++)            printf("%s%c",ans[query[i]-1].str,i!=q-1?' ':'\n');    }    return 0;}


原创粉丝点击