結果

問題 No.3298 K-th Slime
ユーザー Nafmo2
提出日時 2025-10-05 15:29:39
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 1,182 bytes
コンパイル時間 1,919 ms
コンパイル使用メモリ 204,616 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2025-10-05 15:29:52
合計ジャッジ時間 4,737 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N, K, Q;
  cin >> N >> K >> Q;
  vector<ll> A(N);
  for (int i = 0; i < N; ++i) cin >> A[i];
  sort(A.begin(), A.end());
  multiset<ll> pq1, pq2;
  auto push = [&](ll x) {
    if (pq1.size() < K) {
      pq1.insert(x);
    } else {
      if (*pq1.rbegin() <= x) {
        pq2.insert(x);
      } else {
        pq2.insert(*pq1.rbegin());
        pq1.erase(prev(pq1.end()));
        pq1.insert(x);
      }
    }
  };
  for (int i = 0; i < N; i++) {
    push(A[i]);
  }
  while (Q--) {
    ll t;
    cin >> t;
    if (t == 1) {
      ll x;
      cin >> x;
      push(x);
    } else if (t == 2) {
      ll x;
      cin >> x;
      auto it = prev(pq1.end());
      ll k = *it;
      if (pq2.empty()) {
        pq1.erase(it);
        pq1.insert(x + k);
        continue;
      }
      if (*pq2.begin() < k + x) {
        pq1.erase(prev(pq1.end()));
        pq1.insert(*pq2.begin());
        pq2.erase(pq2.begin());
        pq2.insert(x + k);
      } else {
        pq1.erase(prev(pq1.end()));
        pq1.insert(x + k);
      }
    } else {
      cout << *pq1.rbegin() << "\n";
    }
  }
  return 0;
}
0