結果
問題 | No.649 ここでちょっとQK! |
ユーザー |
|
提出日時 | 2023-06-27 17:50:57 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,107 bytes |
コンパイル時間 | 1,107 ms |
コンパイル使用メモリ | 90,492 KB |
実行使用メモリ | 14,444 KB |
最終ジャッジ日時 | 2024-07-04 10:28:29 |
合計ジャッジ時間 | 5,812 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 WA * 1 |
other | AC * 4 WA * 28 |
ソースコード
#include <iostream> #include <queue> #include <unordered_map> using namespace std; using ll = long long; using vll = vector<ll>; struct DynamicPriorityQueue { priority_queue<ll> small; // small.top() は small の最大値 (K個になるようにする) priority_queue<ll, vector<ll>, greater<ll>> large; // large.top() は large の最小値 unordered_map<ll, int> elementCounts; // 要素の出現回数を管理するハッシュマップ int K; void add(ll value) { if (small.size() < K) { small.push(value); } else { ll current_kth = small.top(); if (value < current_kth) { small.pop(); small.push(value); large.push(current_kth); } else { large.push(value); } } elementCounts[value]++; } ll getKthSmallest() { if (small.size() < K) { return -1; // K番目の値が存在しない場合は-1を返す } ll current_kth = small.top(); return current_kth; } void remove(ll x) { if (elementCounts.find(x) != elementCounts.end()) { elementCounts[x]--; if (elementCounts[x] == 0) { elementCounts.erase(x); } } // largeにxが存在する場合は、smallの最小値と交換する if (!large.empty() && large.top() == x) { large.pop(); small.push(x); } } }; int main() { DynamicPriorityQueue dq; int Q, K; cin >> Q >> K; dq.K = K; vll ans; for (int i = 0; i < Q; ++i) { int T; cin >> T; if (T == 1) { ll V; cin >> V; dq.add(V); } else { ll kthSmallest = dq.getKthSmallest(); ans.push_back(kthSmallest); dq.remove(kthSmallest); } } for (int i = 0; i < ans.size(); ++i) { cout << ans[i] << endl; } return 0; }