优先队列 HDOJ 5437 Alisha's Party

题目传送门

题意:一人过生日,很多人排着队送礼物。排队顺序是礼物价值大的优先,如果相等先来的优先。有m次开门,当t个人到了会开门让p个人进门。最后一次让门外的所有人按顺序进门。有q次询问,问第x个进门的人的名字。

分析:很明显的优先队列,首先交给队友做,结果写的搓,无限RE。没办法只能我上,因为要对字符串处理我用了string,思路正确,各种坑都解决了但是很快WA了,我的内心是奔溃的。赛后发现是cin,cout和scanf,printf冲突了 (ios::sync_with_stdio (false);关闭同步)

,以前听说过这个问题,这次终于碰上了!解题思路是先排好序,t也是要排序的,而且有相同的t,没到t个人来,那么入队t个人,注意队列中不一定有p个人,及时break,详细见代码。

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

const int N = 150000 + 10;
const int INF = 0x3f3f3f3f;
struct People    {
    string name;
    int v, id;
    People () {}
    People (string n, int v, int id) : name (n), v (v), id (id) {}
    bool operator < (const People &r) const {
        if (v == r.v)    return id > r.id;
        else    return v < r.v;
    }
}p[N], ans[N];
struct Op    {
    int t, p;
    bool operator < (const Op &r) const {
        if (t == r.t)    return p < r.p;
        else return t < r.t;
    }
}a[N];

int main(void)    {
    ios::sync_with_stdio (false);        //用了这句话,puts都不能用了
    int T;    cin >> T;
    while (T--)    {
        int n, m, q;    cin >> n >> m >> q;
        for (int i=1; i<=n; ++i)    {
            cin >> p[i].name >> p[i].v;    p[i].id = i;
        }
        for (int i=1; i<=m; ++i)    {
            cin >> a[i].t >> a[i].p;
        }
        sort (a+1, a+1+m);

        int tot = 0;
        priority_queue<People> Q;
        int n1 = 0, n2 = 1;
        while (!Q.empty () || n1 <= n)    {
            while (n2 <= m && n1 == a[n2].t)    {
                for (int i=1; i<=a[n2].p; ++i)    {
                    if (Q.empty ())    break;
                    ans[++tot].name = Q.top ().name;    Q.pop ();
                }
                n2++;
            }
            n1++;
            if (n1 > n)    break;
            Q.push (People (p[n1].name, p[n1].v, p[n1].id));
        }
        while (!Q.empty ())    {
            ans[++tot].name = Q.top ().name;    Q.pop ();
        }
        for (int x, i=1; i<=q; ++i)    {
            cin >> x;
            cout << ans[x].name;
            if (i == q)    cout << endl;
            else    cout << " ";
        }
    }

    return 0;
}

  

编译人生,运行世界!
原文地址:https://www.cnblogs.com/Running-Time/p/4806322.html