結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー 👑 tatt61880tatt61880
提出日時 2021-03-26 23:09:19
言語 Kuin
(KuinC++ v.2021.9.17)
結果
AC  
実行時間 835 ms / 2,000 ms
コード長 3,999 bytes
コンパイル時間 3,450 ms
コンパイル使用メモリ 172,544 KB
実行使用メモリ 49,012 KB
最終ジャッジ日時 2023-10-14 17:57:45
合計ジャッジ時間 13,500 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 3 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,356 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 26 ms
6,156 KB
testcase_14 AC 26 ms
6,220 KB
testcase_15 AC 25 ms
6,020 KB
testcase_16 AC 25 ms
6,024 KB
testcase_17 AC 25 ms
6,260 KB
testcase_18 AC 25 ms
6,256 KB
testcase_19 AC 25 ms
5,980 KB
testcase_20 AC 25 ms
6,268 KB
testcase_21 AC 25 ms
6,008 KB
testcase_22 AC 25 ms
6,204 KB
testcase_23 AC 833 ms
48,704 KB
testcase_24 AC 835 ms
48,884 KB
testcase_25 AC 829 ms
48,712 KB
testcase_26 AC 814 ms
48,760 KB
testcase_27 AC 811 ms
48,812 KB
testcase_28 AC 739 ms
48,948 KB
testcase_29 AC 738 ms
48,868 KB
testcase_30 AC 784 ms
49,012 KB
testcase_31 AC 740 ms
48,732 KB
testcase_32 AC 798 ms
48,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

func main()
	const invalid: int :: lib@intMax
	var n: int :: cui@inputInt()
	var a: []int :: #[n]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt()
	end for
	var lMin: []int :: #[n]int
	var rMin: []int :: #[n]int
	block
		var min: int :: a[0]
		for i(1, n - 1)
			do lMin[i] :: min
			do min :: [min, a[i]].min()
		end for
	end block
	block
		var min: int :: a[n - 1]
		for i(n - 2, 0, -1)
			do rMin[i] :: min
			do min :: [min, a[i]].min()
		end for
	end block
	var lLow: []int :: #[n]int
	var rLow: []int :: #[n]int
	block
		var set: Set :: #Set
		do set.add(a[0])
		for i(1, n - 1)
			var p: Node :: set.lower_bound(a[i])
			do lLow[i] :: p =& null ?(-1, p.key)
			do set.add(a[i])
		end for
	end block
	block
		var set: Set :: #Set
		do set.add(a[n - 1])
		for i(n - 2, 0, -1)
			var p: Node :: set.lower_bound(a[i])
			do rLow[i] :: p =& null ?(-1, p.key)
			do set.add(a[i])
		end for
	end block
	
	var ans: int :: invalid
	for i(1, n - 2)
		var v: int :: a[i]
		; ^
		var lm: int :: lMin[i]
		var rm: int :: rMin[i]
		if(lm < v & v > rm)
			do ans :: [ans, lm + v + rm].min()
		end if
		; v
		var ll: int :: lLow[i]
		var rl: int :: rLow[i]
		if(ll > v & v < rl)
			do ans :: [ans, ll + v + rl].min()
		end if
	end for
	if(ans = invalid)
		do ans :: -1
	end if
	do cui@print("\{ans}\n")
	
	class Node()
		+var height: int
		+var key: int
		+var lst: Node
		+var rst: Node
		+*func toStr(): []char
			ret "\{me.key}"
		end func
		+func init(height: int, key: int): Node
			do me.height :: height
			do me.key :: key
			do me.lst :: null
			do me.rst :: null
			ret me
		end func
	end class
	func height(t: Node): int
		ret t =& null ?(0, t.height)
	end func
	func bias(t: Node): int
		ret height(t.lst) - height(t.rst)
	end func
	func modHeight(t: Node)
		do t.height :: 1 + lib@max(height(t.lst), height(t.rst))
	end func
	func rotateL(v: Node): Node
		var u: Node :: v.rst
		var t: Node :: u.lst
		do u.lst :: v
		do v.rst :: t
		ret u
	end func
	func rotateR(u: Node): Node
		var v: Node :: u.lst
		var t: Node :: v.rst
		do v.rst :: u
		do u.lst :: t
		ret v
	end func
	func rotateLR(t: Node): Node
		do t.lst :: rotateL(t.lst)
		ret rotateR(t)
	end func
	func rotateRL(t: Node): Node
		do t.rst :: rotateR(t.rst)
		ret rotateL(t)
	end func
	
	; AVL Tree
	class Set()
		var root: Node
		var change: bool
		var lmax: int
		+func balanceL(t: Node): Node
			if(!me.change)
				ret t
			end if
			var h: int :: height(t)
			if(bias(t) = 2)
				if(bias(t.lst) >= 0)
					do t :: rotateR(t)
				else
					do t :: rotateLR(t)
				end if
			else
				do modHeight(t)
			end if
			do me.change :: (h <> height(t))
			ret t
		end func
		+func balanceR(t: Node): Node
			if(!me.change)
				ret t
			end if
			var h: int :: height(t)
			if(bias(t) = -2)
				if(bias(t.rst) <= 0)
					do t :: rotateL(t)
				else
					do t :: rotateRL(t)
				end if
			else
				do modHeight(t)
			end if
			do me.change :: (h <> height(t))
			ret t
		end func
		+func add(key: int)
			do me.root :: me.addSub(me.root, key)
		end func
		+func addSub(t: Node, key: int): Node
			if(t =& null)
				do me.change :: true
				ret(#Node).init(1, key)
			elif(key < t.key)
				do t.lst :: me.addSub(t.lst, key)
				ret me.balanceL(t)
			elif(key > t.key)
				do t.rst :: me.addSub(t.rst, key)
				ret me.balanceR(t)
			else
				do me.change :: false
				ret t
			end if
		end func
		+func find(key: int): Node
			var t: Node :: me.root
			while loop(t <>& null)
				if(key < t.key)
					do t :: t.lst
				elif(key > t.key)
					do t :: t.rst
				else
					break loop
				end if
			end while
			ret t
		end func
		+func lower_bound(key: int): Node
			var t: Node :: me.root
			if(t =& null)
				ret null
			end if
			while(true)
				if(key < t.key)
					if(t.lst =& null)
						ret t
					end if
					if(key > t.lst.key)
						ret t
					end if
					do t :: t.lst
				elif(key > t.key)
					if(t.rst =& null)
						ret null
					end if
					do t :: t.rst
				else
					ret t
				end if
			end while
		end func
	end class
end func
0