Ultraviolet A 501-Black Box (priority queue)
Ultraviolet A 501-Black Box
Question Link
Given some operations, add a number each time, and then output the current number smaller than I during the I-th query.
Idea: using two priority queues is equivalent to dividing the sequence into two sequences. The answer for each query is the header of the next queue.
Code:
#include
#include
#include
#include
using namespace std;const int N = 30005;int t, m, q, vis[N], num[N];void solve() { priority_queue
a; priority_queue
, greater
> b; for (int i = 1; i <= m; i++) {a.push(num[i]);b.push(a.top());a.pop();while (vis[i]--) { printf("%d\n", b.top()); a.push(b.top()); b.pop();} }}int main() { scanf("%d", &t); while (t--) {scanf("%d%d", &m, &q);for (int i = 1; i <= m; i++) scanf("%d", &num[i]);memset(vis, 0, sizeof(vis));while (q--) { scanf("%d", &num[0]); vis[num[0]]++;}solve();if (t) printf("\n"); } return 0;}