hdoj 5437 Alisha’s Party 【优先队列 模拟】
2015-09-14 19:14
363 查看
Alisha’s PartyTime Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1216 Accepted Submission(s): 346 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−thfriends 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 |
现在给出q次查询,每次查询让你找到第n个进入宫殿的人输出他的名字。
思路:优先队列模拟所有人进入宫殿的过程并记录第i个通过的人的编号。
注意:给出的M次门打开的条件t[i] 和 p[i],有可能存在t[i] > t[i+1],所以要先按t值升序排列。
AC代码:
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define MAXN 150005 using namespace std; struct Node { char name[201]; int val, pos; friend bool operator < (Node a, Node b) { if(a.val != b.val) return a.val < b.val;//优先权值大的 else return a.pos > b.pos; } }num[MAXN]; int order[MAXN]; struct rec { int t, p; }; rec door[MAXN]; bool cmp(rec a, rec b) { return a.t < b.t; } int main() { int t; int K, M, q; scanf("%d", &t); while(t--) { priority_queue<Node> Q; scanf("%d%d%d", &K, &M, &q); for(int i = 1; i <= K; i++) scanf("%s%d", num[i].name, &num[i].val); for(int i = 1; i <= M; i++) scanf("%d%d", &door[i].t, &door[i].p); sort(door+1, door+M+1, cmp);//这里要排序。。。 int top = 1, cnt = 1; for(int i = 1; i <= M; i++) { while(cnt <= door[i].t) { Node E; strcpy(E.name, num[cnt].name); E.val = num[cnt].val; E.pos = cnt; Q.push(E); cnt++; } while(door[i].p && !Q.empty()) { order[top++] = Q.top().pos; Q.pop(); door[i].p--; } } //处理最后未进去的所有人 while(cnt <= K) { Node E; strcpy(E.name, num[cnt].name); E.val = num[cnt].val; E.pos = cnt; Q.push(E); cnt++; } while(!Q.empty()) { order[top++] = Q.top().pos; Q.pop(); } int n; for(int i = 0; i < q; i++) { scanf("%d", &n); if(i > 0) printf(" "); printf("%s", num[order ].name); } printf("\n"); } return 0; }
相关文章推荐
- 5件事测你能否活过80岁
- 线程池
- 浅谈加密技术
- OpenCV中对Mat里面depth,dims,channels,step,data,elemSize和数据地址计算的理解
- js校验15位/18位身份证件号(地区、生日、性别)
- android学习之路之通知-Notification
- JSP 对话对象 Session
- LeetCode(56)Merge Intervals
- android .9.png ”点九” 图片制作方法
- 黑马程序员---Java基础---java语言基本组成:语句
- 挂载mount
- LeetCode(56)Merge Intervals
- java常用线程池的特点
- JQuery最好用的API在线文档
- 用map写的电话本增删改查
- 2015 ACM多校训练第四场
- 使用try-catch
- Create CXF Client
- scala中 -> 与 <- 操作符的区别
- HDU1159Common Subsequence DP