結果

問題 No.318 学学学学学
ユーザー code-devocode-devo
提出日時 2016-03-04 19:43:17
言語 Ruby
(3.3.0)
結果
AC  
実行時間 811 ms / 2,000 ms
コード長 1,044 bytes
コンパイル時間 497 ms
コンパイル使用メモリ 11,508 KB
実行使用メモリ 30,240 KB
最終ジャッジ日時 2023-09-04 18:36:02
合計ジャッジ時間 12,456 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
16,056 KB
testcase_01 AC 111 ms
17,148 KB
testcase_02 AC 120 ms
17,288 KB
testcase_03 AC 106 ms
16,848 KB
testcase_04 AC 115 ms
17,376 KB
testcase_05 AC 459 ms
30,224 KB
testcase_06 AC 811 ms
28,700 KB
testcase_07 AC 808 ms
28,264 KB
testcase_08 AC 793 ms
27,760 KB
testcase_09 AC 774 ms
27,352 KB
testcase_10 AC 727 ms
26,812 KB
testcase_11 AC 414 ms
30,240 KB
testcase_12 AC 356 ms
28,104 KB
testcase_13 AC 292 ms
27,148 KB
testcase_14 AC 293 ms
27,096 KB
testcase_15 AC 280 ms
26,676 KB
testcase_16 AC 270 ms
26,268 KB
testcase_17 AC 380 ms
27,700 KB
testcase_18 AC 379 ms
27,948 KB
testcase_19 AC 786 ms
27,996 KB
testcase_20 AC 325 ms
25,848 KB
testcase_21 AC 77 ms
15,144 KB
testcase_22 AC 77 ms
15,112 KB
testcase_23 AC 78 ms
15,172 KB
testcase_24 AC 78 ms
15,120 KB
testcase_25 AC 79 ms
15,308 KB
testcase_26 AC 79 ms
15,140 KB
testcase_27 AC 79 ms
15,104 KB
testcase_28 AC 79 ms
15,032 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

#配列aが昇順にソートされた状態を保ったまま要素nを入れる場合に、
#どの位置にインサートすればよいかを返す。
def insert_index(a, n)
  l = 0
  r = a.length
  return 0 if r == 0

  while l < r do
    m = (l + r) / 2

    case n <=> a[m]
    when 1 then
      return m + 1 if r - l <= 1
      l = m
    when 0 then
      return m + 1
    when -1 then
      return m if r - l <= 1
      r = m
    end
  end
end

gets
a = gets.split.map(&:to_i)

max_idxs = Hash.new{} #数が最後に現れるインデックスを格納する。
a.each.with_index do |n, idx|
  max_idxs[n] = idx
end

ans = []
open_nums = [] #今までに現れた数。要素は昇順にソートされている。出力に不要となった要素は削除される。
a.each.with_index do |n, idx|
  open_nums.insert(insert_index(open_nums, n), n)
  open_nums.pop while open_nums.size > 0 && idx > max_idxs[open_nums[-1]] #末尾の数がもう現れない数の場合、削除する。
  ans << open_nums[-1]
end
puts ans.join(" ")
0