Alisha’s Party(队列)
Alisha’s Party
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2518 Accepted Submission(s): 681
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 queryn 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<stdio.h>
#include<string.h>
#include<queue>
#define ini(x) while(!x.empty())x.pop()
using namespace std;
const int MAXN=;
struct Node{
char s[];
int w,n;
friend bool operator < (Node a,Node b){
if(a.w!=b.w)return a.w<b.w;
else return a.n>b.n;
}
};
priority_queue<Node>dl;
queue<Node>as;
queue<Node>ae;
struct open{
int k,n;
friend bool operator < (open a,open b){
return a.k>b.k;
}
};
priority_queue<open>op;
char ans[MAXN][];
int main(){
int T,k,m,q;
scanf("%d",&T);
while(T--){
ini(as);ini(ae);ini(op);
scanf("%d%d%d",&k,&m,&q);
Node a;
for(int i=;i<=k;i++){
scanf("%s%d",a.s,&a.w);
a.n=i;
as.push(a);
}
open b;
for(int i=;i<=m;i++){
scanf("%d%d",&b.k,&b.n);
op.push(b);
}b=op.top();
for(int i=;i<=k;i++){
a=as.front();
as.pop();
dl.push(a);
if(op.empty())continue;//错到这里了
if(i==b.k){
int t=;
while(t++<b.n){
if(dl.empty())break;
a=dl.top();
dl.pop();
ae.push(a);
}
op.pop();b=op.top();
}
}
while(!dl.empty()){
a=dl.top();
dl.pop();
ae.push(a);
}
int k=;
while(!ae.empty()){
a=ae.front();ae.pop();
strcpy(ans[k++],a.s);
}
int x;
for(int i=;i<q;i++){
scanf("%d",&x);
if(i)printf(" ");
printf("%s",ans[x]);
}
puts("");
}
return ;
}
代码二:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define ini(x) while(!x.empty())x.pop()
using namespace std;
const int MAXN=;
struct Node{
char s[];
int w,n;
friend bool operator < (Node a,Node b){
if(a.w!=b.w)return a.w<b.w;
else return a.n>b.n;
}
};
priority_queue<Node>dl;
Node as[MAXN];
queue<Node>ae;
struct open{
int k,n;
};
open op[MAXN];
int cmp(open a,open b){
if(a.k!=b.k)return a.k<b.k;
else return a.n>b.n;
}
char ans[MAXN][];
int main(){
int T,k,m,q;
scanf("%d",&T);
while(T--){
ini(ae);ini(dl);
scanf("%d%d%d",&k,&m,&q);
Node a;
for(int i=;i<=k;i++){
scanf("%s%d",a.s,&a.w);
a.n=i;
as[i]=a;
}
open b;
for(int i=;i<=m;i++){
scanf("%d%d",&b.k,&b.n);
op[i]=b;
}
sort(op+,op+m+,cmp);
for(int i=,j=;i<=k;i++){
a=as[i];
dl.push(a);
if(j>m)continue;//错到这里了
if(i==op[j].k){
int t=;
while(t++<op[j].n){
if(dl.empty())break;
a=dl.top();
dl.pop();
ae.push(a);
}
j++;
}
}
while(!dl.empty()){
a=dl.top();
dl.pop();
ae.push(a);
}
int k=;
while(!ae.empty()){
a=ae.front();ae.pop();
strcpy(ans[k++],a.s);
}
int x;
for(int i=;i<q;i++){
scanf("%d",&x);
if(i)printf(" ");
printf("%s",ans[x]);
}
puts("");
}
return ;
}
代码三:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN=;
struct Node{
char s[];
int w;
int nm;
};
bool operator < (Node a,Node b){
if(a.w!=b.w)return a.w<b.w;
else return a.nm>b.nm;
}
struct Peo{
int gate,num;
}door[MAXN];
Node man[MAXN];
int cmp(Peo a,Peo b){
return a.gate<b.gate;
}
struct ANS{
char s[];
};
ANS ans[MAXN];
/*void print(int i,int q){
int x;
if(i>q)return;
scanf("%d",&x);
print(i+1,q);
printf("%s",ans[x]);
if(i!=q)printf(" ");
}*/
int main(){
int T,k,m,q;
scanf("%d",&T);
while(T--){
// memset(ans,0,sizeof(ans));
// memset(door,0,sizeof(door));
priority_queue<Node>dl;
queue<Node>as;
scanf("%d%d%d",&k,&m,&q);
for(int i=;i<=k;i++)
scanf("%s%d",man[i].s,&man[i].w),man[i].nm=i;
for(int i=;i<=m;i++)scanf("%d%d",&door[i].gate,&door[i].num);
sort(door+,door+m+,cmp);
for(int i=,j=;i<=k;i++){
//if(!dl.empty())printf("%s\n",dl.top().s);
dl.push(man[i]);
if(j>m)continue;
//if(!dl.empty())printf("%s\n",dl.top().s);
if(i==door[j].gate){
int t=;
while(t<door[j].num){
// printf("%d\n",door[j].num);
if(dl.empty())break;
Node a=dl.top();
as.push(a);
//if(!dl.empty())printf("%d %s\n",t,a.s);
dl.pop();
t++;
}
j++;
} }
// for(int i=1;i<=k;i++)printf("%s ",dl.top().s),dl.pop();
// puts("");
//for(int i=1;i<=k;i++)printf("%s ",as.front().s),as.pop();
while(!dl.empty()){
as.push(dl.top());
dl.pop();
}
int x=;
while(!as.empty()){
Node a;
a=as.front();
as.pop();
strcpy(ans[x].s,a.s);
x++;
}
//print(1,q);
for(int i=;i<=q;i++){
scanf("%d",&x);
if(i!=)printf(" ");
printf("%s",ans[x].s);
}
puts("");
}
return ;
}
Alisha’s Party(队列)的更多相关文章
- 消息队列——RabbitMQ学习笔记
消息队列--RabbitMQ学习笔记 1. 写在前面 昨天简单学习了一个消息队列项目--RabbitMQ,今天趁热打铁,将学到的东西记录下来. 学习的资料主要是官网给出的6个基本的消息发送/接收模型, ...
- 消息队列 Kafka 的基本知识及 .NET Core 客户端
前言 最新项目中要用到消息队列来做消息的传输,之所以选着 Kafka 是因为要配合其他 java 项目中,所以就对 Kafka 了解了一下,也算是做个笔记吧. 本篇不谈论 Kafka 和其他的一些消息 ...
- Beanstalkd一个高性能分布式内存队列系统
高性能离不开异步,异步离不开队列,内部是Producer-Consumer模型的原理. 设计中的核心概念: job:一个需要异步处理的任务,是beanstalkd中得基本单元,需要放在一个tube中: ...
- .net 分布式架构之业务消息队列
开源QQ群: .net 开源基础服务 238543768 开源地址: http://git.oschina.net/chejiangyi/Dyd.BusinessMQ ## 业务消息队列 ##业务消 ...
- 【原创经验分享】WCF之消息队列
最近都在鼓捣这个WCF,因为看到说WCF比WebService功能要强大许多,另外也看了一些公司的招聘信息,貌似一些中.高级的程序员招聘,都有提及到WCF这一块,所以,自己也关心关心一下,虽然目前工作 ...
- 缓存、队列(Memcached、redis、RabbitMQ)
本章内容: Memcached 简介.安装.使用 Python 操作 Memcached 天生支持集群 redis 简介.安装.使用.实例 Python 操作 Redis String.Hash.Li ...
- Java消息队列--ActiveMq 实战
1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...
- Java消息队列--JMS概述
1.什么是JMS JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送 ...
- 消息队列性能对比——ActiveMQ、RabbitMQ与ZeroMQ(译文)
Dissecting Message Queues 概述: 我花了一些时间解剖各种库执行分布式消息.在这个分析中,我看了几个不同的方面,包括API特性,易于部署和维护,以及性能质量..消息队列已经被分 ...
随机推荐
- 习惯使用断言Assert
一直在给党做项目,我们这些可怜兮兮的学生都没太多时间安排自己的活动了,写个blog都要在中午休息的时间. 项目用的是.NET,本来也想分享一些干货点的东西,但博客园里的前辈把这类文章已经分享泛滥了,想 ...
- Android开发:自定义GridView/ListView数据源
http://mobile.51cto.com/android-259861.htm 在开发中,我们常常会遇到比较复杂的GridView/ListView的布局,重新实现BaseAdapter不但能帮 ...
- Qt新建线程的方法(四种办法,很详细,有截图)
看了不少Qt线程的东西,下面总结一下Qt新建一个线程的方法. 一.继承QThread 继承QThread,这应该是最常用的方法了.我们可以通过重写虚函数void QThread::run ()实现我们 ...
- SQL Server带游标的SQL
DECLARE test_cursor CURSOR FOR SELECT ID FROM dbo.T_BD_Restaurant WHERE id <> '0AAB2E55-79F8-4 ...
- mysql 插入前 锁表问题
$dbh = DBI->connect("dbi:mysql:database=$db_name;host=$ip;port=3306",$user,$passwd,{ Ra ...
- chrome可以登陆账号的hosts文件
原文地址: 百度 chrome吧 http://zhidao.baidu.com/question/1818688600091435508.html?qq-pf-to=pcqq.group http: ...
- 多项式ADT的数组实现
/*删除表的正确方法*/ /*assume header*/ void DeleteList(List L) { Position p,Tmp; p=L->Next; while(p != NU ...
- floodlight 学习(一)
其实这个控制器应该没有多少人用了吧,一年多都没更新了,鉴于最近无论如何都要用这个,将学习笔记贴出来吧. 1.FloodlightProvider(Dev) 1.1简介:FloodlightProvid ...
- 飘逸的python - 两种with语句实现方法
第一种是实现上下文管理器协议,即魔法方法__enter__和__exit__. class Foo: def __enter__(self): print 'in' def __exit__(self ...
- 从头开始-01.C语言环境测试
在Mac下编写C程序需要以下几步: 编写代码 a>编译:把C语言编译成0和1 b>工具:clang编译器 c>指令:cc -c 文件名.c 编译成功会生成一个. o目标文件 ...