Blackops

初心易得,始终难守

0%

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

Alisha’s Party

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

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 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

题目链接:HDU 5437
就是一到模拟题,但是题目看漏了个条件最后一定会开门导致了几次次…,加上一开始用string输入输出还T了几发(惨)
一看就感觉是个模拟题,但是肯定要稍微优化一下时间复杂度,那么我们肯定要顺序处理出先后到来的人然后直接回答询问,用优先队列模拟排队,然后每一次开门从队列里取出$p$个人,注意当队列为空就要$break$了,然后由于最后一定会开门,那直接在开门时间后面再加一组${n,\infty }$询问即可,$query[]$忘记映射又WA几发……阿席巴
代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 150010;
struct info
{
char name[205];
int val, id;
bool operator<(const info &rhs)const
{
return (val < rhs.val) || (val == rhs.val && id > rhs.id);
}
} arr[N];
struct data
{
int t, p;
bool operator<(const data &rhs)const
{
return t < rhs.t;
}
} T[N];

int ans[N];
int query[N];
priority_queue<info>Q;

int main(void)
{
int TC, k, m, q, i;
scanf("%d", &TC);
while (TC--)
{
while (!Q.empty())
Q.pop();
scanf("%d%d%d", &k, &m, &q);
for (i = 1; i <= k; ++i)
{
scanf("%s%d", arr[i].name, &arr[i].val);
arr[i].id = i;
}
for (i = 1; i <= m; ++i)
scanf("%d%d", &T[i].t, &T[i].p);
T[++m].p = INF;
T[m].t = k;
sort(T + 1, T + m + 1);
int maxq = 0;
for (i = 1; i <= q; ++i)
{
scanf("%d", &query[i]);
maxq = max(maxq, query[i]);
}
int cur = 1;
int id = 0;
CLR(ans, -1);
for (i = 1; i <= m && id <= maxq; ++i)
{
while (cur <= T[i].t && cur <= k)
Q.push(arr[cur++]);
if (cur == k + 1)
{
while (!Q.empty())
{
ans[++id] = Q.top().id;
Q.pop();
if (id > maxq)
break;
}
}
else
{
while (T[i].p && !Q.empty())
{
--T[i].p;
ans[++id] = Q.top().id;
Q.pop();
}
}
}
for (i = 1; i <= q; ++i)
printf("%s%c", arr[ans[query[i]]].name, " \n"[i == q]);
}
return 0;
}