求调!
查看原帖
求调!
564518
chenmou2012楼主2025/1/23 23:09
#include<bits/stdc++.h>
using namespace std;


priority_queue<int, vector<int>, greater<>> pq;
queue<string> ops;
int n,cnt=0;
signed main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		string cmd;
		cin >> cmd;
		if (cmd == "insert") {
			int x;
			cin >> x;
			pq.push(x);
			ops.push("insert " + to_string(x));
			cnt++;
		} else if (cmd == "removeMin") {
			if (pq.empty()) {
				ops.push("insert 0");
				ops.push("removeMin");
				cnt++;
				cnt++;
			} else {
				ops.push("removeMin");
				cnt++;
				pq.pop();
			}
		} else if (cmd == "getMin") {
			int x;
			cin >> x;
			if (pq.empty()) {
				pq.push(x);
				cnt++;
				ops.push("insert " + to_string(x));
			} else {
				if (pq.top() == x) {
					cnt++;
					ops.push("getMin " + to_string(x));
				}
				if (pq.top() > x) {
					pq.push(x);
					cnt++;
					ops.push("insert "+to_string(x));
				} else {
					while (pq.top() < x && !pq.empty()) {
						pq.pop();
						cnt++;
						ops.push("removeMin");
					}
					if (pq.top() == x && !pq.empty()) {
						continue;
					} else {
						pq.push(x);
						cnt++;
						ops.push("insert " + to_string(x));
					}
				}
			}
			cnt++;
			ops.push("getMin " + to_string(x));
		}
	}
	cout << cnt << endl;
	while (!ops.empty()) {
		cout << ops.front() << endl;
		ops.pop();
	}
	return 0;
}
2025/1/23 23:09
加载中...