結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー 小野寺健小野寺健
提出日時 2021-11-04 20:55:21
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 2,023 bytes
コンパイル時間 40 ms
コンパイル使用メモリ 7,680 KB
実行使用メモリ 107,716 KB
最終ジャッジ日時 2024-04-23 03:42:55
合計ジャッジ時間 8,079 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
19,360 KB
testcase_01 AC 77 ms
12,160 KB
testcase_02 AC 82 ms
12,288 KB
testcase_03 AC 78 ms
12,160 KB
testcase_04 WA -
testcase_05 AC 82 ms
12,160 KB
testcase_06 AC 78 ms
12,160 KB
testcase_07 AC 79 ms
12,288 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 76 ms
12,288 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 229 ms
16,512 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 233 ms
16,512 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

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

class SegmentTree
	def initialize(a)
		@a = a
		n = 1
		while n < @a.length do
			n *= 2
		end
		@dat = Array.new(2*n-1) {Array.new}
		init(0, 0, a.length)
	end
	def merge(a, b)
		c = []
		while a.length > 0 and b.length > 0 do
			if a[0] <= b[0] then
				c << a.shift
			else
				c << b.shift
			end
		end
		if a.length > 0 then
			c += a
		else
			c += b
		end
		c
	end
	def init(k, l, r)
		if r - l == 1 then
			@dat[k] << @a[l]
		else
			lch = k * 2 + 1
			rch = k * 2 + 2
			init(lch, l, (l + r) / 2)
			init(rch, (l + r) / 2, r)
			@dat[k] = merge(@dat[lch].dup, @dat[rch].dup)
		end
	end
	def query(i, j, x, k, l, r)
		if j <= l or r <= i then
			return [nil, nil] 
		elsif i <= l and r <= j then
			min0 = @dat[k][0] < x ? @dat[k][0] : nil
			return [min0, @dat[k].bsearch{|y| y > x}]
		end
		lc0, lc1 = query(i, j, x, k * 2 + 1, l, (l + r) / 2)
		rc0, rc1 = query(i, j, x, k * 2 + 2, (l + r) / 2, r)
		min0 = lc0 ? (rc0 ? [lc0, rc0].min : lc0) : (rc0 ? rc0 : nil)
		min1 = lc1 ? (rc1 ? [lc1, rc1].min : lc1) : (rc1 ? rc1 : nil)
		return [min0, min1]
	end
	def dat
		@dat
	end
end

st = SegmentTree.new(A)
min = Float::INFINITY
m0 = l0 = l1 = r0 = r1 = nil
1.upto(N-2) {|i|
	m1 = A[i]
	update_l = update_r = false
	if not m0 then
		l0 = A[0] if A[0] < m1
		l1 = A[0] if A[0] > m1
		update_r = true
	else
		if not l0 then
			l0 = m0 if m0 < m1
		elsif l0 > m0
			l0 = m0
		end
		if not l1 then
			l1 = m0 if m0 > m1
		elsif l1 < m1 and m0 < m1 then
			update_l = true
		elsif l1 > m1 and m0 > m1 then
			l1 = m0 if m0 < l1
		else
			l1 = m0 if m0 > l1
		end
		
		if not r0 or r0 == m1 then
			update_r = true
		end
		if not r1 or r1 >= m1 or m0 < m1 then
			update_r = true
		end
	end
	l0, l1 = st.query(0, i, m1, 0, 0, N) if update_l
	r0, r1 = st.query(i+1, N, m1, 0, 0, N) if update_r
	if l0 and r0 then
		x = l0 + m1 + r0
		min = x if min > x
	end
	if l1 and r1 then
		x = l1 + m1 + r1
		min = x if min > x
	end
	m0 = m1
}

puts min == Float::INFINITY ? -1 : min
0