HDU 5437 & ICPC 2015 Changchun Alisha's Party(优先队列)
Alisha’s Party
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 7971 Accepted Submission(s): 1833
Description
Princess Alisha invites her friends to come to her birthday party. Each of her friends will bring a gift of some value v, and all of them will come at a different time. Because the lobby is not large enough, Alisha can only let a few people in at a time. She decides to let the person whose gift has the highest value enter first.
Each time when Alisha opens the door, she can decide to let p people enter her castle. If there are less than p people in the lobby, then all of them would enter. And after all of her friends has arrived, Alisha will open the door again and this time every friend who has not entered yet would enter.
If there are two friends who bring gifts of the same value, then the one who comes first should enter first. Given a query n Please tell Alisha who the n−th person to enter her castle is.
Input
The first line of the input gives the number of test cases, T , where 1≤T≤15.
In each test case, the first line contains three numbers k,m and q separated by blanks. k is the number of her friends invited where 1≤k≤150,000. The door would open m times before all Alisha’s friends arrive where 0≤m≤k. Alisha will have q queries where 1≤q≤100.
The i−th of the following k lines gives a string Bi, which consists of no more than 200 English characters, and an integer vi, 1≤vi≤108, separated by a blank. Bi is the name of the i−th person coming to Alisha’s party and Bi brings a gift of value vi.
Each of the following m lines contains two integers t(1≤t≤k) and p(0≤p≤k) separated by a blank. The door will open right after the t−th person arrives, and Alisha will let p friends enter her castle.
The last line of each test case will contain q numbers n1,...,nq separated by a space, which means Alisha wants to know who are the n1−th,...,nq−th friends to enter her castle.
Note: there will be at most two test cases containing n>10000.
Output
For each test case, output the corresponding name of Alisha’s query, separated by a space.
Sample Input
1
5 2 3
Sorey 3
Rose 3
Maltran 3
Lailah 5
Mikleo 6
1 1
4 2
1 2 3
Sample Output
题目大意
Alisha要举办一个party,会有k个来宾,每个来宾会有一个价值为vi的礼物,但是Alisha不能同时接待这么多人,于是她会分好几次打开大门,放几个人进来,题目会给出m对数,分别是t和p,即在第t个人到达后,打开大门,在外面排队的前p个人进来。排队的方式是根据礼物的价值从高到低排,价值一样的先来的站前面。最后所有的人都会进来。最后给出q次查询,询问第几个进来的人是谁。
比如题目中,Sorey先来,Alisha在第一个人来到之后,打开门放一个人进来,也就是Sorey。然后Rose、Maltran、Lailah都来了,因为Lailah的礼物贵重,所以他排在前面,Maltran的礼物和Rose一样,排在Rose后面,现在Alisha开门放前两个人进来,也就是Lailah和Rose。然后Mikleo拿着价值为6的礼物站到了Maltran的前面,最后他们都进来了,于是进门的顺序是:Sorey Lailah Rose Mikleo Maltran.对于三次查询 输出的是前三个人的名字。
分析
这个题目显然是要采用优先队列,但是我总是RE,之后看了别人的代码才恍然大悟:题目给出的m对数,t不一定是按顺序来的,也就是说样例中的 1 1 和 4 2 是可以反过来的!!所以不能在输入的时候就完成模拟,这是我一直RE的原因(原来RE也有可能是这样的.....小弱鸡学到了)
代码实现
#include<bits/stdc++.h> using namespace std; typedef struct node { int vul; int num; char names[205]; friend bool operator < (const node & a,const node & b) { return a.vul < b.vul || (a.vul == b.vul && a.num > b.num); } }vistor; priority_queue <vistor> pq; vistor v[150005]; int anss[150005],w[150005]; int main() { int t; cin>>t; while(t--) { memset(v,0,sizeof(v)); memset(anss,0,sizeof(anss)); memset(w,0,sizeof(w)); while(!pq.empty()) pq.pop(); int k,m,q,i,j,s=1,r=0,x,y; scanf("%d%d%d",&k,&m,&q); for(i=1;i<=k;i++) { scanf("%s",v[i].names); scanf("%d",&v[i].vul); v[i].num=i; } for(i=1;i<=m;i++) { scanf("%d %d",&x,&y); w[x]+=y; } for(i=1;i<=k;i++) { pq.push(v[i]); while(w[i]--) { if(!pq.empty()) { anss[s++]=pq.top().num; pq.pop(); } } } while(!pq.empty()) { anss[s++]=pq.top().num; pq.pop(); } for(i=1;i<=q;i++) { scanf("%d",&x); if(i!=q) printf("%s ",v[anss[x]].names); else printf("%s\n",v[anss[x]].names); } } }