結果

問題 No.649 ここでちょっとQK!
ユーザー どららどらら
提出日時 2018-02-09 22:54:45
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 295 ms / 3,000 ms
コード長 1,270 bytes
コンパイル時間 1,889 ms
コンパイル使用メモリ 176,280 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-08 17:21:18
合計ジャッジ時間 6,403 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

struct typeA {
  ll v;
};
bool operator<(const typeA& a, const typeA& b){
  return a.v < b.v;
}
struct typeB {
  ll v;
};
bool operator<(const typeB& a, const typeB& b){
  return a.v > b.v;
}

class Kth {
  int K;
  int sz;
  priority_queue<typeA> aque;
  priority_queue<typeB> bque;
public:
  Kth(int K):sz(0),K(K){}

  int size() const { return sz; }
  void add(ll v){
    sz++;
    aque.push((typeA){v});
    if(aque.size() <= K) return;
    typeA x = aque.top(); aque.pop();
    bque.push((typeB){x.v});
  }
  ll pop(){
    if(aque.size() < K) return -1;
    sz--;
    typeA x = aque.top(); aque.pop();
    if(bque.size() > 0){
      typeB y = bque.top(); bque.pop();
      aque.push((typeA){y.v});
    }
    return x.v;
  }
};



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

  Kth kth(K);
  rep(i,Q){
    int type;
    cin >> type;
    if(type == 1){
      ll v;
      cin >> v;
      kth.add(v);
    }else{
      cout << kth.pop() << endl;
    }
  }
  return 0;
}


0