백준 알고리즘 7662번 : 이중 우선순위 큐

Zoo Da·2021년 11월 9일
0

백준 알고리즘

목록 보기
250/337
post-thumbnail

링크

https://www.acmicpc.net/problem/7662

sol1) Multiset

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define fastio cin.tie(0)->sync_with_stdio(0)
#define sz(v) (int)(v).size()
#define all(v) v.begin(), v.end()
#define rall(v) (v).rbegin(), (v).rend()
#define compress(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define OOB(x, y) ((x) < 0 || (x) >= n || (y) < 0 || (y) >= m)
#define debug(x) cout << (#x) << ": " << (x) << '\n'
using namespace std;
using ll = long long;
using ull = unsigned long long;
using dbl = double;
using ldb = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vs = vector<string>;
using tii = tuple<int,int,int>;
template<typename T> using wector = vector<vector<T>>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const ll LNF = 1e18 + 7;

int main() {
  fastio;
  int tc; cin >> tc;
  while(tc--){
    int n; cin >> n;
    multiset<int> MS; // increasing order
    for(int i = 0; i < n; i++){
      char c; int x; cin >> c >> x;
      if(c == 'D'){
        if(MS.empty()) continue;
        if(x == 1) MS.erase(prev(MS.end()));
        else MS.erase(MS.begin());
      }
      else MS.insert(x);
    }
    if(MS.empty()) cout << "EMPTY" << "\n";
    else cout << *prev(MS.end()) << ' ' << *MS.begin() << "\n";
  }
  return 0;
}

중복된 값이 들어올 수 있기 때문에 multiset을 활용해야하며, 비어있는 도중에 삭제 명령이 들어오면 건너 뛰어줘야하는 것에 주의해주어야합니다.
그 후엔 문제에서 나와있는 데로 구현해주면 됩니다.

profile
메모장 겸 블로그

0개의 댓글