結果

問題 No.649 ここでちょっとQK!
ユーザー 0w1
提出日時 2020-03-13 15:19:40
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 427 ms / 3,000 ms
コード長 867 bytes
コンパイル時間 2,538 ms
コンパイル使用メモリ 196,116 KB
最終ジャッジ日時 2025-01-09 06:38:37
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
  int Q, K;
  cin >> Q >> K;

  multiset<int64_t> bag0;
  multiset<int64_t> bag1;
  auto balance = [&]() {
    while (bag0.size() < K && bag1.size()) {
      bag0.insert(*bag1.begin());
      bag1.erase(bag1.begin());
    }
    while (bag0.size() > K) {
      bag1.insert(*--bag0.end());
      bag0.erase(--bag0.end());
    }
  };
  while (Q--) {
    int P;
    cin >> P;
    if (P == 1) {
      int64_t V;
      cin >> V;
      bag0.insert(V);
      if (bag1.size() && *bag1.begin() < *--bag0.end()) {
        bag1.insert(*--bag0.end());
        bag0.erase(--bag0.end());
      }
      balance();
    } else {
      if (bag0.size() == K) {
        cout << *--bag0.end() << endl;
        bag0.erase(--bag0.end());
      } else {
        cout << -1 << endl;
      }
      balance();
    }
  }

  return 0;
}
0