码迷,mamicode.com
首页 > 其他好文 > 详细

Alisha’s Party(队列)

时间:2015-09-23 20:50:15      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

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


Problem Description
Princess Alisha invites her friends to come to her birthday party. Each of her friends will bring a gift of some valuev技术分享, 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 queryn技术分享 Please tell Alisha who the nth技术分享 person to enter her castle is.
 

 

Input
The first line of the input gives the number of test cases,T技术分享 , where 1T15技术分享.

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 1k150,000技术分享. The door would open m times before all Alisha’s friends arrive where 0mk技术分享. Alisha will have q技术分享 queries where 1q100技术分享.

The ith技术分享 of the following k技术分享 lines gives a string B技术分享i技术分享技术分享, which consists of no more than 200技术分享 English characters, and an integer v技术分享i技术分享技术分享,1v技术分享i技术分享10技术分享8技术分享技术分享, separated by a blank. B技术分享i技术分享技术分享 is the name of the ith技术分享 person coming to Alisha’s party and Bi brings a gift of value v技术分享i技术分享技术分享.

Each of the following m技术分享 lines contains two integers t(1tk)技术分享 and p(0pk)技术分享 separated by a blank. The door will open right after the tth技术分享 person arrives, and Alisha will let p技术分享 friends enter her castle.

The last line of each test case will contain q技术分享 numbers n技术分享1技术分享,...,n技术分享q技术分享技术分享 separated by a space, which means Alisha wants to know who are the n技术分享1技术分享th,...,n技术分享q技术分享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
题解;交了20多遍。。。。
两个代码:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 #define ini(x) while(!x.empty())x.pop()
 5 using namespace std;
 6 const int MAXN=150010;
 7 struct Node{
 8     char s[210];
 9     int w,n;
10     friend bool operator < (Node a,Node b){
11         if(a.w!=b.w)return a.w<b.w;
12         else return a.n>b.n;
13     }
14 };
15 priority_queue<Node>dl;
16 queue<Node>as;
17 queue<Node>ae;
18 struct open{
19     int k,n;
20     friend bool operator < (open a,open b){
21         return a.k>b.k;
22     }
23 };
24 priority_queue<open>op;
25 char ans[MAXN][210];
26 int main(){
27     int T,k,m,q;
28     scanf("%d",&T);
29     while(T--){
30         ini(as);ini(ae);ini(op);
31         scanf("%d%d%d",&k,&m,&q);
32         Node a;
33         for(int i=1;i<=k;i++){
34             scanf("%s%d",a.s,&a.w);
35             a.n=i;
36             as.push(a);
37         }
38         open b;
39         for(int i=1;i<=m;i++){
40             scanf("%d%d",&b.k,&b.n);
41             op.push(b);
42         }b=op.top();
43         for(int i=1;i<=k;i++){
44             a=as.front();
45             as.pop();
46             dl.push(a);
47             if(op.empty())continue;//错到这里了 
48             if(i==b.k){
49                 int t=0;
50                 while(t++<b.n){
51                     if(dl.empty())break;
52                     a=dl.top();
53                     dl.pop();
54                     ae.push(a);
55                 }
56                 op.pop();b=op.top();
57             }
58         }
59         while(!dl.empty()){
60             a=dl.top();
61             dl.pop();
62             ae.push(a);
63         }
64         int k=1;
65         while(!ae.empty()){
66             a=ae.front();ae.pop();
67             strcpy(ans[k++],a.s);
68         }
69         int x;
70         for(int i=0;i<q;i++){
71             scanf("%d",&x);
72             if(i)printf(" ");
73             printf("%s",ans[x]);
74         }
75         puts("");
76     }
77     return 0;
78 }

代码二:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 #include<algorithm>
 5 #define ini(x) while(!x.empty())x.pop()
 6 using namespace std;
 7 const int MAXN=150010;
 8 struct Node{
 9     char s[210];
10     int w,n;
11     friend bool operator < (Node a,Node b){
12         if(a.w!=b.w)return a.w<b.w;
13         else return a.n>b.n;
14     }
15 };
16 priority_queue<Node>dl;
17 Node as[MAXN];
18 queue<Node>ae;
19 struct open{
20     int k,n;
21 };
22 open op[MAXN];
23 int cmp(open a,open b){
24     if(a.k!=b.k)return a.k<b.k;
25     else return a.n>b.n;
26 }
27 char ans[MAXN][210];
28 int main(){
29     int T,k,m,q;
30     scanf("%d",&T);
31     while(T--){
32     ini(ae);ini(dl);
33         scanf("%d%d%d",&k,&m,&q);
34         Node a;
35         for(int i=1;i<=k;i++){
36             scanf("%s%d",a.s,&a.w);
37             a.n=i;
38             as[i]=a;
39         }
40         open b;
41         for(int i=1;i<=m;i++){
42             scanf("%d%d",&b.k,&b.n);
43             op[i]=b;
44         }
45         sort(op+1,op+m+1,cmp);
46         for(int i=1,j=1;i<=k;i++){
47             a=as[i];
48             dl.push(a);
49             if(j>m)continue;//错到这里了 
50             if(i==op[j].k){
51                 int t=0;
52                 while(t++<op[j].n){
53                     if(dl.empty())break;
54                     a=dl.top();
55                     dl.pop();
56                     ae.push(a);
57                 }
58                 j++;
59             }
60         }
61         while(!dl.empty()){
62             a=dl.top();
63             dl.pop();
64             ae.push(a);
65         }
66         int k=1;
67         while(!ae.empty()){
68             a=ae.front();ae.pop();
69             strcpy(ans[k++],a.s);
70         }
71         int x;
72         for(int i=0;i<q;i++){
73             scanf("%d",&x);
74             if(i)printf(" ");
75             printf("%s",ans[x]);
76         }
77         puts("");
78     }
79     return 0;
80 }

 

 

Alisha’s Party(队列)

标签:

原文地址:http://www.cnblogs.com/handsomecui/p/4833309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!