Alisha’s Party

Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 5471    Accepted Submission(s): 1370

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
思路:直接用优先队列模拟。注意:m可以为0。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
const int MAXN=;
struct Node{
char name[];
int val,id;
Node(){}
bool operator<(const Node &nod) const
{
if(val!=nod.val) return val < nod.val;
else return id > nod.id;
}
}node[MAXN];
struct Query{
int t,q;
}que[MAXN];
int n,m,q;
int query[MAXN];
bool comp(Query q1,Query q2)
{
return q1.t < q2.t;
}
int res[MAXN],tot;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
tot=;
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<=n;i++)
{
scanf("%s %d",node[i].name,&node[i].val);
node[i].id=i;
}
for(int i=;i<m;i++)
{
scanf("%d%d",&que[i].t,&que[i].q);
}
int mx=;
for(int i=;i<q;i++)
{
scanf("%d",&query[i]);
mx=max(mx,query[i]);
}
sort(que,que+m,comp);
priority_queue<Node> pque;
for(int i=,j=;i<=n;i++)
{
pque.push(node[i]);
if(j<m&&i==que[j].t)
{
for(int l=;l<que[j].q&&!pque.empty();l++)
{
Node nod=pque.top();pque.pop();
res[++tot]=nod.id;
}
j++;
}
if(tot>=mx) break;
}
while(!pque.empty())
{
Node nod=pque.top();pque.pop();
res[++tot]=nod.id;
if(tot>=mx) break;
}
for(int i=;i<q-;i++)
{
printf("%s ",node[res[query[i]]].name);
}
printf("%s\n",node[res[query[q-]]].name);
}
return ;
}

HDOJ5437(优先队列)的更多相关文章

  1. 堆排序与优先队列——算法导论(7)

    1. 预备知识 (1) 基本概念     如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...

  2. 数据结构:优先队列 基于list实现(python版)

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...

  3. python优先队列,队列和栈

    打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...

  4. 数据结构作业——Sanji(优先队列)

    山治的婚约 Description 我们知道,山治原来是地下有名的杀人家族文斯莫克家族的三子,目前山治的弟弟已经出现,叫做四治,大哥二哥就叫汪(One)治跟突(Two)治好了(跟本剧情无关) .山治知 ...

  5. Java优先队列

    按照Java api的说法: java.util.PriorityQueue.PriorityQueue() Creates a PriorityQueue with the default init ...

  6. 优先队列实现Huffman编码

    首先把所有的字符加入到优先队列,然后每次弹出两个结点,用这两个结点作为左右孩子,构造一个子树,子树的跟结点的权值为左右孩子的权值的和,然后将子树插入到优先队列,重复这个步骤,直到优先队列中只有一个结点 ...

  7. “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)

    题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...

  8. Dijkstra算法优先队列实现与Bellman_Ford队列实现的理解

    /* Dijkstra算法用优先队列来实现,实现了每一条边最多遍历一次. 要知道,我们从队列头部找到的都是到 已经"建好树"的最短距离以及该节点编号, 并由该节点去更新 树根 到其 ...

  9. 数据结构作业——ギリギリ eye(贪心+优先队列/贪心+并查集)

    ギリギリ eye Description A.D.1999,由坠落地球的“谜之战舰”带来的 Over Technology,揭示了人类历史和远古文明之间的丝丝联系, 促使人类终止彼此间的战争,一方面面 ...

随机推荐

  1. 006PHP基础知识——数据类型(三)

    <?php /** * 数据类型(三) * PHP是一个弱类型的语言 */ //检测数据类型:gettype() 返回字符串的数据类型 /*$str="美丽中国"; echo ...

  2. laravel怎么获取到public路径

    app_path() app_path函数返回app目录的绝对路径: $path = app_path(); 你还可以使用app_path函数为相对于app目录的给定文件生成绝对路径: $path = ...

  3. js 预处理 与 执行 的顺序

    链接 浏览器渲染 js先编译, 后执行, 而且先在第一块代码段编译执行, 再到第二个代码段, 代码段之间是可以共享变量的 DOMContentLoaded

  4. [Shell]bash的良好编码实践

    最好的bash脚本不仅可以工作,而且以易于理解和修改的方式编写.很多好的编码实践都是来自使用一致的变量名称和一致的编码风格.验证用户提供的参数是否正确,并检查命令是否能成功运行,以及长时间运行是否能保 ...

  5. SpringMVC札集(05)——SpringMVC参数回显

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  6. keras系列︱seq2seq系列相关实现与案例(feedback、peek、attention类型)

    之前在看<Semi-supervised Sequence Learning>这篇文章的时候对seq2seq半监督的方式做文本分类的方式产生了一定兴趣,于是开始简单研究了seq2seq.先 ...

  7. 机器学习算法实现解析——libFM之libFM的训练过程之Adaptive Regularization

    本节主要介绍的是libFM源码分析的第五部分之二--libFM的训练过程之Adaptive Regularization的方法. 5.3.Adaptive Regularization的训练方法 5. ...

  8. KAFKA 0.11 RHEL6.5安装

    KAFKA简介 KAFKA是一款分布式消息发布和订阅的系统. 官网:http://kafka.apache.org/ 1.下载KAFKA及JDK KAFKA下载地址: http://kafka.apa ...

  9. quick2.2.6 问题记录

    1.luasocket 不能使用方式 用下面地址的文件替换文件重新编译 https://github.com/chukong/quick-cocos2d-x/blob/master/lib/cocos ...

  10. php pdo调用SQLServer存储过程无法获取返回结果

    确定存储过程写的没问题,php调用后,跟踪了语句,也是没问题,就是获取不到返回结果.折腾,搞定. 较之前明确了1. 调用存储过程传参的写法: 2. 获取返回结果集的方法 参考: http://blog ...