알고리즘 문제풀이
[백준/BOJ] 백준 7662번 : 이중 우선순위 큐
GeniusJo
2020. 9. 23. 02:59
7662번: 이중 우선순위 큐
입력 데이터는 표준입력을 사용한다. 입력은 T개의 테스트 데이터로 구성된다. 입력의 첫 번째 줄에는 입력 데이터의 수를 나타내는 정수 T가 주어진다. 각 테스트 데이터의 첫째 줄에는 Q에 적��
www.acmicpc.net
multiset을 이용하여 이중 우선순위 큐를 표현했다
코드
#include <iostream>
#include <algorithm>
#include <set>
#include <string>
using namespace std;
int main()
{
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int tc;
int k;
string command;
int n;
multiset<int> q; //multiset을 이용하여 이중 우선순위 큐를 표현했다
multiset<int>::iterator it;
cin >> tc;
for (int t = 0; t < tc; t++)
{
q.clear();
cin >> k;
for (int i = 0; i < k; i++)
{
cin >> command >> n;
if (command == "I")
{
q.insert(n);
}
if (command == "D")
{
//q가 비어있을때는 무시
if (q.empty())
continue;
//최댓값을 삭제한다
if (n == 1)
{
it = q.end();
it--;
q.erase(it);
}
//최솟값을 삭제한다
else if (n == -1)
{
it = q.begin();
q.erase(it);
}
}
}
//q가 비어있을때
if (q.empty())
cout << "EMPTY" << "\n";
else
{
it = q.end();
it--;
cout << *it << " "; //최댓값 출력
it = q.begin();
cout << *it; //최솟값 출력
cout << "\n";
}
}
return 0;
}