【优先队列】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;
}
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;
}
相关文章推荐
- Java的多线程机制系列:不得不提的volatile及指令重排序
- 职业规划课第二次作业
- scrollTop clientTop offsetTop scrollHeight clientHeight clientWidth的差别及使用方法
- 如何通过javascript提交表单form
- 多线程之指令重排序
- 防火墙
- MyEclipse快捷键大全
- XMPP即时聊天步骤
- hdu 3336、4763、2594 kmp next数组的理解、模板题
- 获取本机IP
- 【宽搜】Vijos P1206 CoVH之再破难关
- android颜色
- jsp中的basePath
- 概率论与数理统计 Day.2
- 学习进度条
- elasticsearch 简要指南-安装及简单测试
- CSS行高——line-height 文本的垂直居中(display:block;情况下)
- 概率论与数理统计 Day.1
- 用消息队列和消息应用状态表来消除分布式事务
- iOS开发过程中触摸事件的处理(hitTest的使用)