您的位置:首页 > 其它

【优先队列】HDU5437Alisha’s Party【2015 ACM/ICPC Asia Regional Changchun Online】

2016-03-22 20:10 288 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5437

Problem 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

Sorey Lailah Rose

题目意思:Alisha有k个朋友来他家,每个朋友来是有先后次序的,Alisha会开m次门,每次都是在第t个朋友到了以后,他会邀请p个朋友进家门,原则是邀请所带礼物价值高的朋友先进门,如果两个朋友的礼物价值相同,就叫先到的进门。最后等所有朋友都到了以后,Alisha会把他所有的朋友都叫进来,进来的顺序跟先前的要求一样。题目给出q个询问,问第ni个进门的朋友是谁。

题目坑点很多,回忆当时比赛的时候,好像就是因为没有考虑到给t,p排序导致这道题目卡了很久!!!

代码:

#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
struct node{
string name;
int value;
int id;
friend bool operator <(node a,node b){
if(a.value!=b.value) return a.value<b.value;
else return a.id>b.id;
}
}stu[150050],ans[150050];
int d[150];
struct node1{
int a,b;
}s[150050];
bool cmp(node1 A,node1 B)
{
return A.a<B.a;
}
int main()
{
cin.sync_with_stdio(false); // 不用会超时!
int t,n,m,k,a,b,c;
cin>>t;
while(t--){
priority_queue<node>q;
int cnt=0,p=0;
cin>>n>>m>>k; // n表示有n个人被邀请,m表示有m次开门,k表示最后询问的人数
for(int i=0;i<n;i++){ // 先将来人的顺序用结构体存储;
cin>>stu[i].name>>stu[i].value;
stu[i].id=i;
}
for(int i=0;i<m;i++) cin>>s[i].a>>s[i].b; // a,b分别表示来第t个人的时候进p人!
sort(s,s+m,cmp); // 这里需要注意,题目没有说明给你的t是从小到大的顺序,所以需要我们排一遍顺序!
for(int i=0;i<m;i++){
a=s[i].a;b=s[i].b;
while(a>cnt) q.push(stu[cnt++]);
while(b&&!q.empty()){ // 注意,这里需要判断队列是否为空!!
ans[p++]=q.top();
q.pop();
b--;
}
}
while(cnt<n) q.push(stu[cnt++]); // 将还没有入队的入队;
while(!q.empty()){ // 最后将还在队列里已经排好序的抛出到ans的结构体中;
ans[p++]=q.top();
q.pop();
}
for(int i=0;i<k;i++) cin>>d[i];
for(int i=0;i<k-1;i++) cout<<ans[d[i]-1].name<<' ';
cout<<ans[d[k-1]-1].name<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: