코딩테스트/Baekjoon
[백준 #7662] 이중 우선순위 큐 (C++)
동띵
2021. 8. 25. 20:30
https://www.acmicpc.net/problem/7662
7662번: 이중 우선순위 큐
입력 데이터는 표준입력을 사용한다. 입력은 T개의 테스트 데이터로 구성된다. 입력의 첫 번째 줄에는 입력 데이터의 수를 나타내는 정수 T가 주어진다. 각 테스트 데이터의 첫째 줄에는 Q에 적
www.acmicpc.net
처음에 이중 우선순위 큐라는 제목을 보고
priority_queue를 사용해 코드를 짜느라 많은 시간을 소비했다.
우선순위 큐보다는 multiset을 사용하면 더 간단하게 풀 수 있는 것 같다.
#include <iostream>
#include <set>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int t, k, n;
char c;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> k;
multiset<int> m;
for (int j = 0; j < k; j++) {
cin >> c >> n;
if (c == 'I') {
m.insert(n);
}
else {
if (!m.empty() && n == -1) {
m.erase(m.begin());
}
else if (!m.empty() && n == 1) {
auto iter = m.end();
iter--;
m.erase(iter);
}
}
}
if (m.empty()) {
cout << "EMPTY" << "\n";
}
else {
auto it = m.end();
it--;
cout << *it << " " << *m.begin() << "\n";
}
}
return 0;
}