代码求调!
查看原帖
代码求调!
551630
Lemon_zqp楼主2024/3/19 21:14

qwq

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

priority_queue<int, vector<int>, greater<int>> q;
map<int, string> ma;

int main(){
	int n, cnt = 1;
	cin >> n;
	while(n--){
		string s;
		cin >> s;
		if(s == "insert"){
			int x;
			cin >> x;
			q.push(x);
			ma[cnt] = "intsert " + to_string(x);
			cnt++;
		}
		else if(s == "removeMin"){
			if(!q.empty()){
				ma[cnt] = "removeMin";
				cnt++;
				q.pop();
			}
			else{
				ma[cnt] = "insert 0";
				cnt++;
				ma[cnt] = "removeMin";
				cnt++;
			}
		}
		else{
			int x;
			cin >> x;
			if(q.empty()){
				ma[cnt] = "insert " + to_string(x);
				cnt++;
				q.push(x);
				ma[cnt] = "getMin " + to_string(x);
				cnt++;
			}
			else if(q.top() == x){
				ma[cnt] = "getMin " + to_string(x);
				cnt++;
			}
			else if(q.top() > x){
				ma[cnt] = "insert " + to_string(x);
				cnt++;
				q.push(x);
				ma[cnt] = "getMin " + to_string(x);
				cnt++;
			}
			else{
				while(q.top() < x){
					ma[cnt] = "removeMin";
					cnt++;
					q.pop();
				}
				if(q.top() == x){
					ma[cnt] = "getMin " + to_string(x);
					cnt++;
				}
				else if(q.top() > x || q.empty()){
					ma[cnt] = "insert " + to_string(x);
					cnt++;
					ma[cnt] = "getMin " + to_string(x);
					cnt++;
				}
			}
		}
		cout << cnt - 1 << endl;
		for(map<int, string>::iterator it = ma.begin(); it != ma.end(); it++)
	   {
	       cout << it -> second << endl;
	   }
	}
	return 0;
}
2024/3/19 21:14
加载中...