백준 7662 이중 우선순위 큐

supway·2022년 2월 26일
0

백준

목록 보기
36/62

백준 7662
set을 이용해서 풀었다.
우선순위 큐를 최대힙과 최소힙 두개 준비해서 풀어도 된다.

#include <bits/stdc++.h>
#include<set>
using namespace std;
int t;
int main() {
	ios::sync_with_stdio(0); cin.tie(0);

	cin >> t;

	while (t--) {

		multiset<int> s;
		int n;
		cin >> n;


		while (n--) {
			string s1;
			int k;
			cin >> s1 >> k;

			if (s1 == "I") {
				s.insert(k);
			}
			else {
				if (k == 1) {
					if (s.empty()) continue;
					auto it = --s.end();
					s.erase(it);
					
				}
				else {
					if (s.empty()) continue;
					auto it = s.begin();
					s.erase(it);
				}
			}
		}
		if (s.empty()) cout << "EMPTY" << '\n';
		else {
			cout << *(--s.end()) << ' ' << *(s.begin()) << '\n';
		}
	}
}
profile
개발잘하고싶은사람

0개의 댓글