天天看点

HDU 5437 Alisha’s Party(优先队列+模拟)

题目地址:点击打开链接

题意:小明有k个朋友,他家太小,他过生日的时候每次只能让一部分人进,开m次门,每次当第t个人来的时候,他会让p个人进(这p个人的礼物价值必须是来的人里面高的那几个,如果他们带的礼物的价值相同,则先来的进),而他有q次询问,第i个进来的人是谁

思路:参考大神的代码看懂了思路,结果写了自己的代码wrong了无数发,最后直接对着大神的代码改,改到倒数第二行一个变量写错了,改了直接就A了,都是泪啊

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>

using namespace std;

const int maxn = 150010;

struct node
{
    int value;
    int id;
    bool operator < (const node &b) const
    {
        if(value != b.value)
            return value < b.value;
        else
            return id > b.id;
    }
}a[maxn];

int C[maxn],I[maxn];
char name[maxn][210];

int main()
{
    int t;
    int N,M,Q,i;
    struct node ll;
    priority_queue<node> que;
    scanf("%d",&t);
    while(t--)
    {
        memset(C,0,sizeof(C));
        scanf("%d%d%d",&N,&M,&Q);
        for(i=1; i<=N; i++)
        {
            scanf("%s%d",name[i],&a[i].value);
            a[i].id = i;
        }
        int u,v;
        for(i=0; i<M; i++)
        {
            scanf("%d%d",&u,&v);
            C[u] = v;
        }
        int p = 0,n = 0;
        for(i=1; i<=N; i++)
        {
            que.push(a[i]);
            n = C[i];
            while(!que.empty() && n)
            {
                ll = que.top();
                I[++p] = ll.id;
                que.pop();
                n--;
            }
        }
        while(!que.empty())
        {
            ll = que.top();
            I[++p] = ll.id;
            que.pop();
        }
        int x;
        for(i=1; i<=Q; i++)
        {
            scanf("%d",&x);
            printf("%s%c",name[I[x]],i == Q ? '\n' : ' ');
        }
    }
    return 0;
}
           

大神地址: 点击打开链接

继续阅读