結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー siman
提出日時 2020-12-17 17:00:25
言語 Ruby
(3.4.1)
結果
TLE  
実行時間 -
コード長 824 bytes
コンパイル時間 104 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 43,036 KB
最終ジャッジ日時 2024-09-21 08:08:49
合計ジャッジ時間 6,958 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20 TLE * 1 -- * 9
権限があれば一括ダウンロードができます
コンパイルメッセージ
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