結果
問題 | No.1095 Smallest Kadomatsu Subsequence |
ユーザー |
|
提出日時 | 2021-03-27 13:23:25 |
言語 | Kuin (KuinC++ v.2021.9.17) |
結果 |
AC
|
実行時間 | 835 ms / 2,000 ms |
コード長 | 4,842 bytes |
コンパイル時間 | 3,155 ms |
コンパイル使用メモリ | 149,432 KB |
実行使用メモリ | 61,696 KB |
最終ジャッジ日時 | 2024-09-16 12:09:44 |
合計ジャッジ時間 | 12,360 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
func main()const invalid: int :: lib@intMaxvar n: int :: cui@inputInt()var a: []int :: #[n]intfor i(0, n - 1)do a[i] :: cui@inputInt()end forvar lMin: []int :: #[n]intvar rMin: []int :: #[n]intblockvar min: int :: a[0]for i(1, n - 1)do lMin[i] :: mindo min :: [min, a[i]].min()end forend blockblockvar min: int :: a[n - 1]for i(n - 2, 0, -1)do rMin[i] :: mindo min :: [min, a[i]].min()end forend blockvar lLow: []int :: #[n]intvar rLow: []int :: #[n]intblockvar set: Set :: #Setdo 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 forend blockblockvar set: Set :: #Setdo 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 forend blockvar ans: int :: invalidfor 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; vvar ll: int :: lLow[i]var rl: int :: rLow[i]if(ll > v & v < rl)do ans :: [ans, ll + v + rl].min()end ifend forif(ans = invalid)do ans :: -1end ifdo cui@print("\{ans}\n")class Node()+var height: int+var key: int+var prev: Node+var next: Node+var lst: Node+var rst: Node+func init(key: int, prev: Node, next: Node): Nodedo me.height :: 1do me.key :: keydo me.prev :: prevdo me.next :: nextret meend funcend class; AVL Treeclass Set()var root: Nodevar change: boolvar num: int+func size(): intret me.numend func+func begin(): Nodevar t: Node :: me.rootwhile(true)if(t.lst =& null)ret tend ifdo t :: t.lstend whileend funcfunc height(t: Node): intret t =& null ?(0, t.height)end funcfunc bias(t: Node): intret me.height(t.lst) - me.height(t.rst)end funcfunc modHeight(t: Node)do t.height :: 1 + lib@max(me.height(t.lst), me.height(t.rst))end funcfunc rotateL(v: Node): Nodevar u: Node :: v.rstvar t: Node :: u.lstdo u.lst :: vdo v.rst :: tret uend funcfunc rotateR(u: Node): Nodevar v: Node :: u.lstvar t: Node :: v.rstdo v.rst :: udo u.lst :: tret vend funcfunc rotateLR(t: Node): Nodedo t.lst :: me.rotateL(t.lst)ret me.rotateR(t)end funcfunc rotateRL(t: Node): Nodedo t.rst :: me.rotateR(t.rst)ret me.rotateL(t)end func+func balanceL(t: Node): Nodeif(!me.change)ret tend ifvar h: int :: me.height(t)if(me.bias(t) = 2)if(me.bias(t.lst) >= 0)do t :: me.rotateR(t)elsedo t :: me.rotateLR(t)end ifelsedo me.modHeight(t)end ifdo me.change :: (h <> me.height(t))ret tend func+func balanceR(t: Node): Nodeif(!me.change)ret tend ifvar h: int :: me.height(t)if(me.bias(t) = -2)if(me.bias(t.rst) <= 0)do t :: me.rotateL(t)elsedo t :: me.rotateRL(t)end ifelsedo me.modHeight(t)end ifdo me.change :: (h <> me.height(t))ret tend func+func add(key: int)do me.root :: me.addSub(me.root, null, key)end func+func addSub(t: Node, parent: Node, key: int): Nodeif(t =& null)var a: Nodedo me.change :: trueif(parent =& null)do a :: (#Node).init(key, null, null)elif(key < parent.key)do a :: (#Node).init(key, parent.prev, parent)if(parent.prev <>& null)do parent.prev.next :: aend ifdo parent.prev :: aelif(key > parent.key)do a :: (#Node).init(key, parent, parent.next)if(parent.next <>& null)do parent.next.prev :: aend ifdo parent.next :: aend ifdo me.num :+ 1ret aelif(key < t.key)do t.lst :: me.addSub(t.lst, t, key)ret me.balanceL(t)elif(key > t.key)do t.rst :: me.addSub(t.rst, t, key)ret me.balanceR(t)elsedo me.change :: falseret tend ifend func+func find(key: int): Nodevar t: Node :: me.rootwhile loop(t <>& null)if(key < t.key)do t :: t.lstelif(key > t.key)do t :: t.rstelsebreak loopend ifend whileret tend func+func exist(key: int): boolret me.find(key) <>& nullend func+func lower_bound(key: int): Nodevar t: Node :: me.rootif(t =& null)ret nullend ifwhile(true)if(key < t.key)if(t.lst =& null)ret tend ifif(key > t.lst.key & t.lst.rst =& null)ret tend ifdo t :: t.lstelif(key > t.key)if(t.rst =& null)ret nullend ifdo t :: t.rstelseret tend ifend whileend funcend classend func