結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー 小野寺健小野寺健
提出日時 2021-11-05 08:06:24
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 1,265 bytes
コンパイル時間 131 ms
コンパイル使用メモリ 7,296 KB
実行使用メモリ 52,864 KB
最終ジャッジ日時 2024-04-23 15:46:58
合計ジャッジ時間 8,597 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
17,536 KB
testcase_01 AC 87 ms
12,160 KB
testcase_02 AC 92 ms
12,160 KB
testcase_03 AC 87 ms
12,288 KB
testcase_04 AC 88 ms
12,160 KB
testcase_05 AC 87 ms
12,160 KB
testcase_06 AC 86 ms
12,160 KB
testcase_07 AC 89 ms
12,288 KB
testcase_08 AC 89 ms
12,288 KB
testcase_09 AC 92 ms
12,288 KB
testcase_10 AC 89 ms
12,160 KB
testcase_11 AC 86 ms
12,160 KB
testcase_12 AC 87 ms
12,288 KB
testcase_13 AC 261 ms
13,440 KB
testcase_14 AC 263 ms
13,312 KB
testcase_15 AC 266 ms
13,568 KB
testcase_16 AC 264 ms
13,312 KB
testcase_17 AC 264 ms
13,312 KB
testcase_18 AC 267 ms
13,184 KB
testcase_19 AC 266 ms
13,184 KB
testcase_20 AC 266 ms
13,184 KB
testcase_21 AC 264 ms
13,184 KB
testcase_22 AC 268 ms
13,184 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class SegmentTree
	def initialize(arr)
		@h = (arr.length - 1).bit_length
		@n = 2**@h
		@ie = Float::INFINITY
		@tree = Array.new(2*@n, @ie)
		0.upto(arr.length-1) {|i|
			@tree[@n + i] = arr[i]
		}
		(@n-1).downto(1) {|i|
			@tree[i] = [@tree[2 * i], @tree[2 * i + 1]].min
		}
	end
	def set(idx, x)
		idx += @n
		@tree[idx] = x
		while idx > 0 do
			idx >>= 1
			@tree[idx] = [@tree[2 * idx], @tree[2 * idx + 1]].min
		end
	end
	def query(lt, rt)
		lt += @n
		rt += @n
		vl = vr = @ie
		while rt - lt > 0 do
			if lt & 1 != 0 then
				vl = [vl, @tree[lt]].min
				lt += 1
			end
			if rt & 1 != 0 then
				rt -= 1
				vr = [@tree[rt], vr].min
			end
			lt >>= 1
			rt >>= 1
		end
		[vl, vr].min
	end
end

INF = Float::INFINITY

N = gets.to_i
A = gets.split(" ").map{|s| s.to_i}

S = A.map.with_index{|x, i| [x, i]}.sort!

res = INF

st1 = SegmentTree.new([INF] * N)

0.upto(N-1) {|i|
	a, idx = S[i]
	st1.set(idx, a)
	lt = st1.query(0, idx)
	rt = st1.query(idx + 1, N)
	next if lt == INF or rt == INF
	res = [res, lt + rt + a].min
}

st2 = SegmentTree.new([INF] * N)

(N-1).downto(0) {|i|
	a, idx = S[i]
	st2.set(idx, a)
	lt = st2.query(0, idx)
	rt = st2.query(idx + 1, N)
	next if lt == INF or rt == INF
	res = [res, lt + rt + a].min
}

puts res != INF ? res : -1
0