Ultraviolet A 501-Black Box (priority queue)
Link to the question: Ultraviolet A 501-Black Box
There is a set in which the given elements enter the order of the set. Now there are Q queries. Given that each query enters the set after the first few elements, for each I query, the number of I in the output set.
Solution: Use two priority queues for maintenance. If queue a has a higher priority value and queue B has a lower priority value, before the I-th inquiry, ensure that a queue has I-1 element elements, and the rest is smaller than B, then each time you ask to output the first element of B queue, and put it in queue.
#include
#include
#include
#include using namespace std;const int maxn = 30005;int N, M, arr[maxn], v[maxn];void solve () { priority_queue
a; priority_queue
, greater
> b; for (int i = 1; i <= N; i++) { a.push(arr[i]); b.push(a.top()); a.pop(); while (v[i]--) { printf("%d\n", b.top()); a.push(b.top()); b.pop(); } }}int main () { int cas; scanf("%d", &cas); while (cas--) { int x; memset(v, 0, sizeof(v)); scanf("%d%d", &N, &M); for (int i = 1; i <= N; i++) scanf("%d", &arr[i]); for (int i = 0; i < M; i++) { scanf("%d", &x); v[x]++; } solve(); if (cas) printf("\n"); } return 0;}