zoukankan      html  css  js  c++  java
  • 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


    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 Bi, which consists of no more than 200 English characters, and an integer vi,1vi108, separated by a blank. Bi is the name of the ith person coming to Alisha’s party and Bi brings a gift of value vi.

    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 n1,...,nq separated by a space, which means Alisha wants to know who are the n1th,...,nqth 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多遍。。。。
    有许多人带有权值的礼物来拜访公主,公主会在第ti个人到的时候把门打开瞬间,放ki个人进来,其中进来的顺序是权值最大的先进,如果权值一样大就先来的先进。当人全部到齐后会再次开门让所有人都进来。
    两个代码:
     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 }

     代码三:

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 #include<queue>
     5 using namespace std;
     6 const int MAXN=150010;
     7 struct Node{
     8     char s[210];
     9     int w;
    10     int nm;
    11 };
    12 bool operator < (Node a,Node b){
    13         if(a.w!=b.w)return a.w<b.w;
    14         else return a.nm>b.nm;
    15     }
    16 struct Peo{
    17     int gate,num;
    18 }door[MAXN];
    19 Node man[MAXN];
    20 int cmp(Peo a,Peo b){
    21     return a.gate<b.gate;
    22 }
    23 struct ANS{
    24     char s[210];
    25 };
    26 ANS ans[MAXN];
    27 /*void print(int i,int q){
    28     int x;
    29     if(i>q)return;
    30     scanf("%d",&x);
    31     print(i+1,q);
    32     printf("%s",ans[x]);
    33     if(i!=q)printf(" ");
    34 }*/
    35 int main(){
    36     int T,k,m,q;
    37     scanf("%d",&T);
    38     while(T--){
    39     //    memset(ans,0,sizeof(ans));
    40     //    memset(door,0,sizeof(door));
    41         priority_queue<Node>dl;
    42         queue<Node>as;
    43         scanf("%d%d%d",&k,&m,&q);
    44         for(int i=1;i<=k;i++)
    45             scanf("%s%d",man[i].s,&man[i].w),man[i].nm=i;
    46         for(int i=1;i<=m;i++)scanf("%d%d",&door[i].gate,&door[i].num);
    47         sort(door+1,door+m+1,cmp);
    48         for(int i=1,j=1;i<=k;i++){
    49             //if(!dl.empty())printf("%s
    ",dl.top().s);
    50             dl.push(man[i]);
    51             if(j>m)continue;
    52             //if(!dl.empty())printf("%s
    ",dl.top().s);
    53             if(i==door[j].gate){
    54                 int t=0;
    55                 while(t<door[j].num){
    56                 //    printf("%d
    ",door[j].num);
    57                 if(dl.empty())break;
    58                 Node a=dl.top();
    59                     as.push(a);
    60                     //if(!dl.empty())printf("%d %s
    ",t,a.s);
    61                     dl.pop();
    62                     t++;
    63                 }
    64                 j++;
    65             }
    66             
    67         }
    68     //    for(int i=1;i<=k;i++)printf("%s ",dl.top().s),dl.pop();
    69     //    puts("");
    70         //for(int i=1;i<=k;i++)printf("%s ",as.front().s),as.pop();
    71         while(!dl.empty()){
    72             as.push(dl.top());
    73             dl.pop();
    74         }
    75         int x=1;
    76         while(!as.empty()){
    77             Node a;
    78             a=as.front();
    79              as.pop();
    80              strcpy(ans[x].s,a.s);
    81              x++;
    82         }
    83         //print(1,q);
    84         for(int i=1;i<=q;i++){
    85             scanf("%d",&x);
    86             if(i!=1)printf(" ");
    87             printf("%s",ans[x].s);
    88         }
    89         puts("");
    90     }
    91     return 0;
    92 }
     
  • 相关阅读:
    GET和POST两种基本请求方法的区别
    GET与POST类型接口
    TCP连接与断开详解(socket通信)
    QC02
    QC01
    tcp三次握手和四次挥手
    ssh整合
    redis主从切换
    缓存解释(一级缓存,二级缓存)
    cxf整合spring代码
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4833309.html
Copyright © 2011-2022 走看看