HDOJ5437(优先队列)
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
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.
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.
#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(优先队列)的更多相关文章
- 堆排序与优先队列——算法导论(7)
1. 预备知识 (1) 基本概念 如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...
- 数据结构:优先队列 基于list实现(python版)
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...
- python优先队列,队列和栈
打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...
- 数据结构作业——Sanji(优先队列)
山治的婚约 Description 我们知道,山治原来是地下有名的杀人家族文斯莫克家族的三子,目前山治的弟弟已经出现,叫做四治,大哥二哥就叫汪(One)治跟突(Two)治好了(跟本剧情无关) .山治知 ...
- Java优先队列
按照Java api的说法: java.util.PriorityQueue.PriorityQueue() Creates a PriorityQueue with the default init ...
- 优先队列实现Huffman编码
首先把所有的字符加入到优先队列,然后每次弹出两个结点,用这两个结点作为左右孩子,构造一个子树,子树的跟结点的权值为左右孩子的权值的和,然后将子树插入到优先队列,重复这个步骤,直到优先队列中只有一个结点 ...
- “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)
题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...
- Dijkstra算法优先队列实现与Bellman_Ford队列实现的理解
/* Dijkstra算法用优先队列来实现,实现了每一条边最多遍历一次. 要知道,我们从队列头部找到的都是到 已经"建好树"的最短距离以及该节点编号, 并由该节点去更新 树根 到其 ...
- 数据结构作业——ギリギリ eye(贪心+优先队列/贪心+并查集)
ギリギリ eye Description A.D.1999,由坠落地球的“谜之战舰”带来的 Over Technology,揭示了人类历史和远古文明之间的丝丝联系, 促使人类终止彼此间的战争,一方面面 ...
随机推荐
- L3-010. 是否完全二叉搜索树
L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...
- javax.servlet-api.jar
servlet.jar 是servlet 3.0 版本之前的地址 javax.servlet-api.jar 是servlet 3.0 版本之后的地址
- 《APUE》第7章 进程环境-读书笔记
一.main函数. main函数的原型如下.argc是命令行参数的数目,argv是指向参数的各个指针所构成的数组. int main(int argc, char *argv[]) 当内核执行C程序时 ...
- Bireme:一个 Greenplum数据仓库的增量同步工具
https://hashdatainc.github.io/bireme/ Bireme 是一个 Greenplum / HashData 数据仓库的增量同步工具.目前支持 MySQL.Postgre ...
- Quartz创建多个不同名字的scheduler实例
_http://my.oschina.net/laiweiwei/blog/122280 需求创建多个不同的Scheduler实例,每个实例自主启动.关闭 问题 如果直接用 SchedulerFact ...
- Sql Server- 性能优化辅助指标SET STATISTICS TIME ON和SET STATISTICS IO ON
1.前言 对于优化SQL语句或存储过程,以前主要是用如下语句来判断具体执行时间,但是SQL环境是复杂多变的,下面语句并不能精准判断性能是否提高:如果需要精确知道CPU.IO等信息,就无能为力了. 1 ...
- mysql连接踩坑
本机安装的是wamp,集成了mysql.php.apache.安装了sqlyog客户端. 1.错误代码2003 证明mysql服务没有开启,此时需要开启mysql服务,开启了wamp 2.错误代码10 ...
- 我也说说Emacs吧(1) - Emacs和Vi我们都学
好友幻神的<Emacs之魂>正在火热连载中,群里人起哄要给他捧捧场. 作为一个学习Emacs屡败屡战的用户,这个场还是值得捧一下的.至少我是买了HHKB键盘的... 从我的键盘说起 - 有 ...
- 我个人所有的独立博客wordpress都被挂马
晕,所有的独立博客都中了这个,难怪访问有点慢,今天使用谷歌浏览器 来自知名恶意软件散布者 ***.net 的内容已经插入此网页.现在访问此网页很可能会导致您的计算机受到恶意软件入侵. 恶意软件是一种不 ...
- ranch分析学习(一)
Ranch 是一个tcp处理的程序框架.官方的解释 Ranch is a socket acceptor pool for TCP protocols. 主要目的是提供一个方便,易用,高效,稳定的t ...