hdu 5437 Alisha’s Party 优先队列

Alisha’s Party

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1001&cid=621

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 n−th person to enter her castle is.

Input

The first line of the input gives the number of test cases, T , where 1≤T≤15.

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 1≤k≤150,000. The door would open m times before all Alisha’s friends arrive where 0≤m≤k. Alisha will have q queries where 1≤q≤100.

The i−th of the following k lines gives a string Bi, which consists of no more than 200 English characters, and an integer vi, 1≤vi≤108, separated by a blank. Bi is the name of the i−th person coming to Alisha’s party and Bi brings a gift of value vi.

Each of the following m lines contains two integers t(1≤t≤k) and p(0≤p≤k) separated by a blank. The door will open right after the t−th 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 n1−th,...,nq−th friends 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

HINT

 

题意

 有一个人会开门,然后让拥有礼物价值最高的人进来,然后问你第k个人进来的人是谁

题解:

优先队列咯,模拟模拟就好了

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
#include <bitset>
typedef unsigned char byte;
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin)
#define pi acos(-1)

using namespace std;
const int maxn = 150000 + 500;
typedef pair<int,int>dl;
struct node
{
    int val;
    int idx;
    friend bool operator < (const node & a,const node & b)
    {
        return a.val < b.val || (a.val == b.val && a.idx > b.idx);
    }    
    node(int val,int idx) : val(val) , idx(idx) {}
};

priority_queue<node>qq;
int k , m , q , Time[maxn];
int v[maxn];
char ks[maxn][400];
dl tt[maxn];
int id[maxn];

void initiation()
{
   scanf("%d%d%d",&k,&m,&q);
   for(int i = 1 ; i <= k ; ++ i) scanf("%s%d",ks[i],&v[i]);
   for(int i = 1 ; i <= m ; ++ i) scanf("%d%d",&tt[i].first,&tt[i].second);
   sort(tt + 1 , tt + 1 + m);
   for(int i = 1 ; i <= q; ++ i) scanf("%d",&id[i]);
}


void solve()
{
    int ptr = 1;
    int cot = 0;
    int T = 1;
    for(int i = 1 ; i <= k ; ++ i)
    {
        cot++;
        qq.push(node(v[i],i));
        while(ptr != m+1 && tt[ptr].first <= cot)
        {
            int cv = tt[ptr].second;
            while(cv && !qq.empty())
            {
                node ss = qq.top();qq.pop();
                Time[T++] = ss.idx;
                cv--;
            }
            ptr++;
        }
    }
    while(!qq.empty())
    {
        node ss = qq.top();qq.pop();
        Time[T++] = ss.idx;
    }
    printf("%s",ks[Time[id[1]]]);
    for(int i = 2 ; i <= q ; ++ i) printf(" %s",ks[Time[id[i]]]);
    printf("\n");
}


int main(int argc,char *argv[])
{
    int Case;
    scanf("%d",&Case);
    while(Case--)
    {
        initiation();
        solve();
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值