반응형
https://www.acmicpc.net/problem/7662
7662번: 이중 우선순위 큐
입력 데이터는 표준입력을 사용한다. 입력은 T개의 테스트 데이터로 구성된다. 입력의 첫 번째 줄에는 입력 데이터의 수를 나타내는 정수 T가 주어진다. 각 테스트 데이터의 첫째 줄에는 Q에 적
www.acmicpc.net
#include <bits/stdc++.h>
using namespace std;
struct Node_max {
int idx, value;
bool operator<(const Node_max& b) const {
return value < b.value;
}
};
struct Node_min {
int idx, value;
bool operator<(const Node_min &b) const {
return value > b.value;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for (int T = 0; T < t; T++) {
int k;
cin >> k;
priority_queue<Node_max> max_q;
priority_queue<Node_min> min_q;
vector<bool> ch(k + 1, 0);
int idx = 1;
int q_size = 0;
for (int i = 0; i < k; i++) {
char c;
int val;
cin >> c >> val;
if (c == 'I') {
max_q.push({ idx,val });
min_q.push({ idx,val });
idx++;
}
else {
if (val == -1) {
while (!min_q.empty() && ch[min_q.top().idx]) {
min_q.pop();
}
if (!min_q.empty()) {
ch[min_q.top().idx] = 1;
min_q.pop();
}
}
else {
while (!max_q.empty() && ch[max_q.top().idx]) {
max_q.pop();
}
if (!max_q.empty()) {
ch[max_q.top().idx] = 1;
max_q.pop();
}
}
}
}
int ans = 0;
for (int i = 1; i < idx; i++) {
if (ch[i] == 0) {
ans++;
}
}
if (ans == 0) {
cout << "EMPTY\n";
}
else {
while (!min_q.empty() && ch[min_q.top().idx]) {
min_q.pop();
}
while (!max_q.empty() && ch[max_q.top().idx]) {
max_q.pop();
}
cout << max_q.top().value << ' ' << min_q.top().value << '\n';
}
}
return 0;
}
반응형
'Algorithm > problem' 카테고리의 다른 글
백준 1738번 : 골목길 - 벨만 포드 알고리즘 C++ (0) | 2022.03.16 |
---|---|
백준 11997번 : Load Balancing(Silver) - 누적합, 좌표 압축 C++ (0) | 2022.03.11 |
백준 16139번 : 인간 - 컴퓨터 상호작용 - 구간 합 C++ (0) | 2022.03.10 |
백준 10986 : 나머지 합 - prefix sum C++ (0) | 2022.03.09 |
백준 16236번 : 아기상어 - BFS C++ (0) | 2022.03.09 |