結果

問題 No.318 学学学学学
ユーザー code-devocode-devo
提出日時 2016-03-04 19:58:26
言語 Ruby
(3.3.0)
結果
AC  
実行時間 822 ms / 2,000 ms
コード長 1,113 bytes
コンパイル時間 814 ms
コンパイル使用メモリ 11,236 KB
実行使用メモリ 30,352 KB
最終ジャッジ日時 2023-09-04 18:37:51
合計ジャッジ時間 12,076 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 98 ms
15,992 KB
testcase_01 AC 115 ms
17,108 KB
testcase_02 AC 125 ms
17,280 KB
testcase_03 AC 113 ms
16,840 KB
testcase_04 AC 119 ms
17,236 KB
testcase_05 AC 469 ms
29,796 KB
testcase_06 AC 818 ms
29,320 KB
testcase_07 AC 822 ms
28,336 KB
testcase_08 AC 799 ms
27,240 KB
testcase_09 AC 781 ms
27,232 KB
testcase_10 AC 738 ms
26,836 KB
testcase_11 AC 424 ms
30,352 KB
testcase_12 AC 360 ms
27,840 KB
testcase_13 AC 296 ms
27,248 KB
testcase_14 AC 294 ms
27,020 KB
testcase_15 AC 284 ms
26,576 KB
testcase_16 AC 272 ms
26,204 KB
testcase_17 AC 392 ms
28,380 KB
testcase_18 AC 389 ms
27,872 KB
testcase_19 AC 799 ms
28,828 KB
testcase_20 AC 331 ms
25,800 KB
testcase_21 AC 80 ms
15,304 KB
testcase_22 AC 83 ms
15,056 KB
testcase_23 AC 81 ms
15,252 KB
testcase_24 AC 81 ms
15,164 KB
testcase_25 AC 82 ms
15,028 KB
testcase_26 AC 80 ms
15,052 KB
testcase_27 AC 81 ms
15,028 KB
testcase_28 AC 83 ms
15,152 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

#each.with_indexとeach_with_indexの差をテストしてみる。

#配列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