公式

B - Get Min 解説 by en_translator


This problem can be solved if we can perform an element insertion and minimum-value retrieval against a multi-set.

These operations can be achieved with a data structure called a priority queue. A priority queue is available as a part of standard library in most languages commonly used in competitive programming.

In this problem, the constraints are small enough, so it can be solved naively too.

For example, one can maintain \(C_i\) as the number of balls currently in the bag with \(i\) written on them, or simply maintain the multi-set of the balls in the bag as a sequence instead of a list.

Sample code

#include <bits/stdc++.h>
using namespace std;

int main() {
	int q;
	cin >> q;
	priority_queue<int, vector<int>, greater<>> pq;
	while (q--) {
		int type;
		cin >> type;
		if (type == 1) {
			int x;
			cin >> x;
			pq.push(x);
		}
		else {
			int x = pq.top(); pq.pop();
			cout << x << '\n';
		}
	}
}
#include <bits/stdc++.h>
using namespace std;

int main() {
	int q;
	cin >> q;
	vector<int> c(101);
	while (q--) {
		int type;
		cin >> type;
		if (type == 1) {
			int x;
			cin >> x;
			c[x]++;
		}
		else {
			for (int i = 1; i <= 100; i++) {
				if (c[i] > 0) {
					c[i]--;
					cout << i << '\n';
					break;
				}
			}
		}
	}
}

投稿日時:
最終更新: