Alisha’s Party---hdu5437(模拟+优先队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5437

题意:公主有k个朋友来参加她的生日party,每个人都会带价值为v[i]的礼物过来,在所有人到齐之前公主会打开大门m次,每次开门就是在第t个人到来之后,然后让p个人进入大厅,如果当前人数不足p人,则让所有人都进去,最后在所有人都来到门口时,再次打开门,让剩下所有的人都进入大厅;当然选择礼物价值大的先进入,如果两个人的礼物价值相等,则先来的先进,现在有q个问题,问第ni个进入大厅的人是谁;

我们可以用优先队列存入每次开门可以进去的人,然后按顺序依次进入p个人,模拟,注意细节即可;

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h>

using namespace std;

#define met(a, b) memset(a, b, sizeof(a))
#define N 150003
#define INF 0x3f3f3f3f

typedef long long LL;

struct node
{
    int Time, v;
    char Name[210];
    friend bool operator < (node p, node q)
    {
        if(p.v != q.v)
            return p.v < q.v;
        return p.Time > q.Time;
    }
}a[N];

struct Open
{
    int t, cnt;
    bool friend operator < (Open p, Open q)
    {
        return p.t < q.t;
    }
}b[N];

int n, m, q;

char ans[N][210];

void solve()
{
    priority_queue<node> Q;///优先队列注意优先级;

    node p;

    int j = 1, k = 1;

    for(int i=1; i<=m; i++)
    {
        while(j <= b[i].t && j<=n)///把此次开门能进去的先放入队列;
        {
            Q.push(a[j]);
            j++;
        }
        int num = b[i].cnt;

        while(!Q.empty() && num --)///每次进num个人,如果不足num就是全进入,进入大厅,即出队列;
        {
            p = Q.top();
            Q.pop();
            strcpy(ans[k++], p.Name);
        }
    }
    while(j<=n)///把剩下没有进入队列的放入队列;
    {
        Q.push(a[j]);
        j++;
    }
    while(!Q.empty())///当所有人都到了之后,再次打开门,让所有人都进去;
    {
        p = Q.top();
        Q.pop();
        strcpy(ans[k++], p.Name);
    }
}

int main()
{
    int T;

    scanf("%d", &T);

    while(T--)
    {
        scanf("%d %d %d", &n, &m, &q);
        for(int i=1; i<=n; i++)
        {
            scanf("%s %d", a[i].Name, &a[i].v);
            a[i].Time = i;
        }

        for(int i=1; i<=m; i++)
            scanf("%d %d", &b[i].t, &b[i].cnt);

        sort(b+1, b+m+1);

        solve();

        for(int i=1; i<=q; i++)
        {
            int x;
            scanf("%d", &x);
            printf("%s%c", ans[x], i==q?‘\n‘:‘ ‘);
        }
    }
    return 0;
}

时间: 2024-10-13 15:57:30

Alisha’s Party---hdu5437(模拟+优先队列)的相关文章

hdu 5437 Alisha’s Party 模拟 优先队列

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

Hdu 5437 Alisha’s Party(模拟)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5437 思路:优先队列模拟,注意最后一次门外人全部进入. #include<cstdio> #include<queue> #include<cstring> #include<iostream> #include<algorithm> #define debu using namespace std; const int maxn=150000+50

hdu 4006 The kth great number (优先队列+STB+最小堆)

The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 6637    Accepted Submission(s): 2671 Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a roun

bestcoder#3——Task schedule

Task schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description 有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务.有m个询问,每个询问有一个数字q,表示如果在q时间

TIANKENG’s restaurant

TIANKENG’s restaurant Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 0 Accepted Submission(s): 0 Problem Description TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousand

Argus Beijing 2004 LA3135

[题目讲解] 本题就是简单模拟优先队列的使用. 下面讲讲队列及优先队列的基础知识: 先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对象被放置在尾部,下一个被取出的元素则取自队列的首部.priority_queue特别之处在于,允许用户为队列中存储的元素设置优先级.这种队列不是直接将新元素放置在队列尾部,而是放在比它优先级低的元素前面.标准库默认使用<操作符来确定对象之间的优先级关系,所以如果要使用自定义对象,需要重载 < 操作符.(>表示优先级小的在前,<表示优先级

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

Alisha's Party Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 4075    Accepted Submission(s): 1052 Problem Description Princess Alisha invites her friends to come to her birthday party. Each

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

Alisha’s Party Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2650    Accepted Submission(s): 722 Problem Description Princess Alisha invites her friends to come to her birthday party. Each o

HDU 5437 &amp; ICPC 2015 Changchun Alisha&#39;s Party(优先队列)

Alisha’s Party Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 7971    Accepted Submission(s): 1833 Description Princess Alisha invites her friends to come to her birthday party. Each of her f