HDOJ5437(优先队列) Alisha’s Party

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


Problem Description
Princess Alisha invites her friends to come to her birthday party. Each of her friends will bring a gift of some value h person to enter her castle is.
 
Input
The first line of the input gives the number of test cases, 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
思路:直接用优先队列模拟。注意:m可以为0。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
const int MAXN=150005;
struct Node{
    char name[205];
    int val,id;
    Node(){}
    bool operator<(const Node &nod) const
    {
        if(val!=nod.val)    return val < nod.val;
        else    return id > nod.id;
    }
}node[MAXN];
struct Query{
    int t,q;
}que[MAXN];
int n,m,q;
int query[MAXN];
bool comp(Query q1,Query q2)
{
    return q1.t < q2.t;
}
int res[MAXN],tot;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        tot=0;
        scanf("%d%d%d",&n,&m,&q);
        for(int i=1;i<=n;i++)
        {
            scanf("%s %d",node[i].name,&node[i].val);
            node[i].id=i;
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&que[i].t,&que[i].q);
        }
        int mx=0;
        for(int i=0;i<q;i++)
        {
            scanf("%d",&query[i]);
            mx=max(mx,query[i]);
        }
        sort(que,que+m,comp);
        priority_queue<Node> pque;
        for(int i=1,j=0;i<=n;i++)
        {
            pque.push(node[i]);
            if(j<m&&i==que[j].t)
            {
                for(int l=0;l<que[j].q&&!pque.empty();l++)
                {
                    Node nod=pque.top();pque.pop();
                    res[++tot]=nod.id;
                }
                j++;
            }
            if(tot>=mx)    break;
        }
        while(!pque.empty())
        {
            Node nod=pque.top();pque.pop();
            res[++tot]=nod.id;
            if(tot>=mx)    break;
        }
        for(int i=0;i<q-1;i++)
        {
            printf("%s ",node[res[query[i]]].name);
        }
        printf("%s
",node[res[query[q-1]]].name);
    }
    return 0;
}