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. 【zznu-2173】

    题目链接 题目描述 春天自幼学习人间大道,斩断情缘,现已修成正果,势必要斩尽巨魔.某朝末年,战乱四起,民不聊生,魔界与人间界的封印减弱,n只巨魔趁机降临人间,祸乱百姓.n只巨魔只知扰乱人间,会有天谴降 ...

  2. vim+ctags用法

    vim用法     在VIM编辑器的环境下用":make"就可以编译程序,如果程序中有错误,就会显示出来.          下列命令可以快速定位,并修改错误错误 ":c ...

  3. New Concept English three(10)

    The great ship, Titanic, sailed for New York from Southampton on April 10th, 1912. She was carrying ...

  4. Java 方法重载与方法重写

    方法重载(Overload): 1.在同一个类中 2.方法名相同 3.参数的个数或类型不同 4.与方法的返回类型无关 5.与方法的修饰符无关 方法重写(Override): 方法重写必须是子类继承父类 ...

  5. Mysql双机热备--预备知识

    1.双机热备 对于双机热备这一概念,我搜索了很多资料,最后,还是按照大多数资料所讲分成广义与狭义两种意义来说. 从广义上讲,就是对于重要的服务,使用两台服务器,互相备份,共同执行同一服务.当一台服务器 ...

  6. MySQL在本机无法基于localhost访问的问题解决

    引言: 在本地访问数据库之时,一般使用localhost, 127.0.0.1来进行数据库访问,但是笔者这几天就碰到了只能通过127.0.0.1来访问,但是无法基于localhost来访问,非常之诡异 ...

  7. 安装Oracle 11G Enterprise Manager工具

    1.登录ORACLE数据库 sqlplus / as sysdba; 2.查询实例名 SQL> select instance_name from v$instance; INSTANCE_NA ...

  8. OK335xS knob driver hacking

    /************************************************************************* * OK335xS knob driver hac ...

  9. 【Python学习】Thread笔记(1)

    Python学习笔记 - Thread(1) 标签(空格分隔): python from threading import Thread num = 2000 id_list = [] def do_ ...

  10. C语言变量、函数的作用域及变量的存储方式

    一.变量的作用域和存储方式 在C语言中每个变量都有两种基本属性:数据类型.数据的存储类别. 数据类型很多人都已熟知,例如:字符型(char).整型(int).浮点型(float)等等.存储类别是指数据 ...