結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー simansiman
提出日時 2020-12-17 17:00:25
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 824 bytes
コンパイル時間 58 ms
コンパイル使用メモリ 11,908 KB
実行使用メモリ 44,324 KB
最終ジャッジ日時 2023-10-21 07:00:06
合計ジャッジ時間 7,947 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
16,080 KB
testcase_01 AC 88 ms
16,080 KB
testcase_02 AC 86 ms
16,080 KB
testcase_03 AC 86 ms
16,080 KB
testcase_04 AC 88 ms
16,080 KB
testcase_05 AC 86 ms
16,080 KB
testcase_06 AC 87 ms
16,080 KB
testcase_07 AC 89 ms
16,080 KB
testcase_08 AC 89 ms
16,076 KB
testcase_09 AC 87 ms
16,080 KB
testcase_10 AC 86 ms
16,080 KB
testcase_11 AC 86 ms
16,080 KB
testcase_12 AC 86 ms
16,080 KB
testcase_13 AC 122 ms
16,856 KB
testcase_14 AC 120 ms
16,856 KB
testcase_15 AC 121 ms
16,856 KB
testcase_16 AC 123 ms
16,856 KB
testcase_17 AC 122 ms
16,856 KB
testcase_18 AC 120 ms
16,856 KB
testcase_19 AC 122 ms
16,856 KB
testcase_20 AC 128 ms
16,856 KB
testcase_21 AC 122 ms
16,856 KB
testcase_22 AC 126 ms
16,856 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:60: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

N = gets.to_i
A = gets.split.map(&:to_i)
B = []
MAX_L = Array.new(N)
MIN_L = Array.new(N)
MAX_R = Array.new(N)
MIN_R = Array.new(N)

A.each_with_index do |a, i|
  idx = B.bsearch_index { |b| b > a }

  if idx
    MAX_L[i] = B[idx]
  end

  idx ||= B.size

  if idx - 1 >= 0
    MIN_L[i] = B[0]
  end

  B.insert(idx, a)
end

C = []

A.reverse.each.with_index(1) do |a, i|
  idx = C.bsearch_index { |b| b > a }

  if idx
    MAX_R[-i] = C[idx]
  end

  idx ||= C.size

  if idx - 1 >= 0
    MIN_R[-i] = C[0]
  end

  C.insert(idx, a)
end

ans = Float::INFINITY

N.times do |i|
  a = A[i]

  if MAX_L[i] && MAX_R[i]
    v = a + MAX_L[i] + MAX_R[i]
    ans = v if ans > v
  end

  if MIN_L[i] && MIN_R[i]
    v = a + MIN_L[i] + MIN_R[i]
    ans = v if ans > v
  end
end

if ans == Float::INFINITY
  puts -1
else
  puts ans
end
0