結果

問題 No.649 ここでちょっとQK!
ユーザー ikdikd
提出日時 2018-02-12 17:36:22
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 405 ms / 3,000 ms
コード長 1,010 bytes
コンパイル時間 1,714 ms
コンパイル使用メモリ 154,956 KB
実行使用メモリ 25,492 KB
最終ジャッジ日時 2024-06-12 23:53:18
合計ジャッジ時間 7,675 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

void main(){
  import std.stdio, std.string, std.conv, std.algorithm;
  import std.container;

  int q, k; rd(q, k);
  // auto qmax=new BinaryHeap!(Array!(long))(),
  //      qmin=new BinaryHeap!(Array!(long), "a>b")();
  auto qmax=new RedBlackTree!(long, "a>b", true),
       qmin=new RedBlackTree!(long, "a<b", true);
  while(q--){
    auto args=readln.split.to!(long[]);
    if(args[0]==1){
      auto v=args[1];
      if(qmax.length<k) qmax.insert(v);
      else if(qmax.front<v) qmin.insert(v);
      else{qmin.insert(qmax.front); qmax.removeFront; qmax.insert(v);}
    }else{
      if(qmax.length>=k){
        writeln(qmax.front);
        qmax.removeFront;
        if(qmax.length<k && qmin.length>0){
          qmax.insert(qmin.front);
          qmin.removeFront;
        }
      }else{
        writeln(-1);
      }
    }
  }
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}
0