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. 在Linux下设置定时任务(每分钟执行一次特定的shell脚本)

    在当前用户下,开始编辑定时任务 crontab -e 按键 i 进入编辑模式 输入下述命令 */ * * * * sh /***/*****.sh 然后按键 Esc 退出编辑模式,再输入 wq 保存退 ...

  2. SpringInAction-- 配置Profile Bean

    Profile Bean 使用场景描述: 在开发软件的时候,在数据库方面,往往不是一个库就能解决的,一般分为开发库.测试库.生产库,在这些库设置链接的时候,也会配置其对应的数据. 现有一种方式,就是单 ...

  3. [转载]面试心得与总结---BAT、网易、蘑菇街等

    转载自:http://mp.weixin.qq.com/s?__biz=MzIzMDIxNTQ3NA==&mid=2649111851&idx=1&sn=f43c42f7262 ...

  4. LINUX系统下PXE网络安装虚拟机

    PXE(preboot execute environment),预启动执行环境.由于安装系统的时候,有时候是大批量的安装:这时使用磁盘或虚拟机进行单个安装,效率太差:所以我们开始使用PXE网络安装L ...

  5. sar工具使用详细介绍

    一:命令介绍:参考资料:http://linux.die.net/man/1/sar sar(System ActivityReporter系统活动情况报告)是目前Linux上最为全面的系统性能分析工 ...

  6. Python读取UTF-8编码文件并使用命令行执行时输出结果的问题

    最近参加了由CCF举办的数据挖掘比赛,主办方提供了csv格式的数据文件,由于中文显示乱码的问题,我先用txt文本编辑器将编码改为utf-8格式,但是在读取文件并输出读取结果时发生了问题,代码如下: # ...

  7. 如何实现PLC与THINGWORX工业物联网平台对接

    物联网(Internet of Things),简称 IoT,对于制造商来说,是行业乃至世界范围内的一股变革浪潮.在我们设计和制造的产品中,将会不断嵌入各种软件.传感器和启用 IP 的连接功能.IDC ...

  8. wireshark的提示

    内容:12个wrieshark的提示 1.[Packet size limited during capture] 在捕获数据包大小有限,即包没有抓全 2.[TCP previous segment ...

  9. Qt 使用#define+qDebug()输出调试信息

    /******************************************************************************************* * Qt 使用 ...

  10. Matlab以MEX方式调用C源代码【转载】

    原文地址:http://blog.sina.com.cn/s/blog_468651400100coas.html 这是自己整理的一个对应的文档:<Matlab以MEX方式调用C源代码> ...