結果

問題 No.649 ここでちょっとQK!
ユーザー drken1215
提出日時 2018-02-13 15:48:35
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 302 ms / 3,000 ms
コード長 1,348 bytes
コンパイル時間 850 ms
コンパイル使用メモリ 75,452 KB
実行使用メモリ 5,916 KB
最終ジャッジ日時 2024-11-29 12:56:25
合計ジャッジ時間 5,744 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
using namespace std;

int main() {
  // small.top() は small の最大値 (K個になるようにする)
  priority_queue<long long> small;

  // large.top() は large の最小値
  priority_queue<long long, vector<long long>, greater<long long> > large;
  
  int Q, K;
  cin >> Q >> K;
  for (int i = 0; i < Q; ++i) {
	int T;
	cin >> T;
	if (T == 1) {
	  long long V;
	  cin >> V;

	  // そもそも K 個に達していなかったら small へ
	  if (small.size() < K) {
		small.push(V);
		continue;
	  }
		
	  // k 番目の値
	  long long current_kth = small.top();

	  // 現在の k 番目の値より小さかったら、それをlarge 側へ追い出して V を small に push
	  if (V < current_kth) {
		small.pop();
		small.push(V);
		large.push(current_kth);
	  }
	  // そうでないときは単純に large 側に push
	  else {
		large.push(V);
	  }
	}
	else {
	  // そもそも K 個に達していなかったらダメ
	  if (small.size() < K) {
		cout << -1 << endl;
		continue;
	  }

	  // k 番目の値を small から pop して、large の top を small へ移す
	  long long current_kth = small.top();
	  cout << current_kth << endl;
	  small.pop();
	  if (!large.empty()) {
		long long next_kth = large.top();
		large.pop();
		small.push(next_kth);
	  }
	}
  }
}















0