https://www.acmicpc.net/submit/7662
우선 해당 문제는 바킹독님 강의를 참고하여 이진 검색 트리 알고리즘을 활용한 STL인 multiset을 활용하였다. 이 문제는 Key, value 대응 관계가 필요한건 아니니 map보다는 set이나 multiset(중복허용)이 적합하다. (최솟값 삭제 최댓값 삭제 모두 O(logN))
#include <bits/stdc++.h>
#include <set>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int k;
cin >> k;
multiset<int> ms;
while(k--)
{
char c;
cin >> c;
if (c == 'D')
{
int num;
cin >> num;
if (ms.empty()) continue;
if (num == 1)
{
ms.erase(prev(ms.end()));
}
else if (num == -1)
{
ms.erase(ms.begin());
}
}
else
{
int num;
cin >> num;
ms.insert(num);
}
}
if (ms.empty())
{
cout << "EMPTY" << '\n';
}
else
{
cout << *prev(ms.end()) << ' ' << *(ms.begin()) << '\n';
}
}
}
'CS(Computer Science)지식 > [C++][코딩 테스트] 자료구조 및 알고리즘' 카테고리의 다른 글
백준 21939번: 문제 추천 시스템 Version1 (C++) (1) | 2024.01.31 |
---|---|
백준 1202번: 보석 도둑(C++) (0) | 2024.01.31 |
백준 9375번: 패션왕 신해빈 [C++] (0) | 2024.01.31 |
백준 17219번: 비밀번호 찾기 [C++] (0) | 2024.01.31 |
백준 13414번: 수강신청 [C++] (0) | 2024.01.31 |