結果

問題 No.649 ここでちょっとQK!
ユーザー letrangerjpletrangerjp
提出日時 2018-02-10 18:34:11
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,738 ms / 3,000 ms
コード長 1,489 bytes
コンパイル時間 53 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 18,048 KB
最終ジャッジ日時 2024-04-23 13:23:26
合計ジャッジ時間 20,820 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
12,160 KB
testcase_01 AC 93 ms
12,160 KB
testcase_02 AC 85 ms
12,160 KB
testcase_03 AC 844 ms
15,360 KB
testcase_04 AC 1,738 ms
18,048 KB
testcase_05 AC 563 ms
17,920 KB
testcase_06 AC 362 ms
17,664 KB
testcase_07 AC 89 ms
12,032 KB
testcase_08 AC 86 ms
12,160 KB
testcase_09 AC 88 ms
12,160 KB
testcase_10 AC 92 ms
12,288 KB
testcase_11 AC 86 ms
12,160 KB
testcase_12 AC 436 ms
14,720 KB
testcase_13 AC 394 ms
14,336 KB
testcase_14 AC 400 ms
14,208 KB
testcase_15 AC 558 ms
14,720 KB
testcase_16 AC 406 ms
14,208 KB
testcase_17 AC 666 ms
14,336 KB
testcase_18 AC 744 ms
14,464 KB
testcase_19 AC 825 ms
14,464 KB
testcase_20 AC 907 ms
14,464 KB
testcase_21 AC 981 ms
14,720 KB
testcase_22 AC 1,033 ms
15,488 KB
testcase_23 AC 1,132 ms
15,616 KB
testcase_24 AC 1,181 ms
15,488 KB
testcase_25 AC 1,220 ms
15,744 KB
testcase_26 AC 1,326 ms
17,408 KB
testcase_27 AC 88 ms
12,160 KB
testcase_28 AC 93 ms
12,288 KB
testcase_29 AC 94 ms
12,416 KB
testcase_30 AC 454 ms
14,720 KB
testcase_31 AC 558 ms
14,208 KB
testcase_32 AC 84 ms
12,288 KB
testcase_33 AC 87 ms
12,416 KB
testcase_34 AC 82 ms
12,288 KB
testcase_35 AC 87 ms
12,160 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:79: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

class PriorityQueue
  attr_reader :elements

  def initialize
    @elements = [nil]
  end

  def <<(element)
    @elements << element
    bubble_up(@elements.size - 1)
  end

  def pop
    exchange(1, @elements.size - 1)
    max = @elements.pop
    bubble_down(1)
    max
  end

  def size
    @elements.size-1
  end

  def head
    @elements[1]
  end

  private

  def bubble_up(index)
    parent_index = (index / 2)

    return if index <= 1
    return if @elements[parent_index] >= @elements[index]

    exchange(index, parent_index)
    bubble_up(parent_index)
  end

  def bubble_down(index)
    child_index = (index * 2)

    return if child_index > @elements.size - 1

    not_the_last_element = child_index < @elements.size - 1
    left_element = @elements[child_index]
    right_element = @elements[child_index + 1]
    child_index += 1 if not_the_last_element && right_element > left_element

    return if @elements[index] >= @elements[child_index]

    exchange(index, child_index)
    bubble_down(child_index)
  end

  def exchange(source, target)
    @elements[source], @elements[target] = @elements[target], @elements[source]
  end
end

l = PriorityQueue.new
r = PriorityQueue.new

Q, K = gets.split.map &:to_i

$<.map{|s|
  if s[/^1 /]
    v = $'.to_i
    if l.size < K
      l << v
    elsif v < l.head
      r << -l.pop
      l << v
    else
      r << -v
    end
  else
    if l.size < K
      p -1
    else
      p l.pop
      l << -r.pop if r.size > 0
    end
  end
}
0