hdoj 5437 Alisha’s Party 【优先队列 模拟】



Alisha’s Party

Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1216    Accepted Submission(s): 346


Problem Description
Princess Alisha invites her friends to come to her birthday party. Each of her friends will bring a gift of some value  v, 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 query  n 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,...,nqthfriends 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
 



题意:有K个朋友要来参加公主的聚会,已经给出每个朋友的姓名和他所带礼物的价值。公主的宫殿前有一个特殊的门,这个门会被打开M次,对于第i次它会在第t[i]个人到达时打开,并且最多允许通过p[i]个人。在打开M次门后,公主会打开最后一次,让所有还在外面的人进入。进入宫殿的先后顺序——所带礼物价值大的先进入,若有多个人带的礼物价值相同,按到达的先后顺序进入。

现在给出q次查询,每次查询让你找到第n个进入宫殿的人输出他的名字。



思路:优先队列模拟所有人进入宫殿的过程并记录第i个通过的人的编号。


注意:给出的M次门打开的条件t[i] 和 p[i],有可能存在t[i] > t[i+1],所以要先按t值升序排列。



AC代码:


#include 
#include 
#include 
#include 
#define MAXN 150005
using namespace std;
struct Node
{
    char name[201];
    int val, pos;
    friend bool operator < (Node a, Node b)
    {
        if(a.val != b.val)
            return a.val < b.val;//优先权值大的
        else
            return a.pos > b.pos;
    }
}num[MAXN];
int order[MAXN];
struct rec
{
    int t, p;
};
rec door[MAXN];
bool cmp(rec a, rec b)
{
    return a.t < b.t;
}
int main()
{
    int t;
    int K, M, q;
    scanf("%d", &t);
    while(t--)
    {
        priority_queue Q;
        scanf("%d%d%d", &K, &M, &q);
        for(int i = 1; i <= K; i++)
            scanf("%s%d", num[i].name, &num[i].val);
        for(int i = 1; i <= M; i++)
            scanf("%d%d", &door[i].t, &door[i].p);
        sort(door+1, door+M+1, cmp);//这里要排序。。。
        int top = 1, cnt = 1;
        for(int i = 1; i <= M; i++)
        {
            while(cnt <= door[i].t)
            {
                Node E;
                strcpy(E.name, num[cnt].name);
                E.val = num[cnt].val;
                E.pos = cnt;
                Q.push(E);
                cnt++;
            }
            while(door[i].p && !Q.empty())
            {
                order[top++] = Q.top().pos;
                Q.pop();
                door[i].p--;
            }
        }
        //处理最后未进去的所有人
        while(cnt <= K)
        {
            Node E;
            strcpy(E.name, num[cnt].name);
            E.val = num[cnt].val;
            E.pos = cnt;
            Q.push(E);
            cnt++;
        }
        while(!Q.empty())
        {
            order[top++] = Q.top().pos;
            Q.pop();
        }
        int n;
        for(int i = 0; i < q; i++)
        {
            scanf("%d", &n);
            if(i > 0) printf(" ");
            printf("%s", num[order[n]].name);
        }
        printf("\n");
    }
    return 0;
}



你可能感兴趣的:(STL)