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 ;
}