結果
問題 | No.1095 Smallest Kadomatsu Subsequence |
ユーザー | toyuzuko |
提出日時 | 2020-06-27 14:27:43 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,237 ms / 2,000 ms |
コード長 | 1,658 bytes |
コンパイル時間 | 285 ms |
コンパイル使用メモリ | 87,076 KB |
実行使用メモリ | 112,880 KB |
最終ジャッジ日時 | 2023-09-18 19:25:56 |
合計ジャッジ時間 | 15,024 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 71 ms
71,264 KB |
testcase_01 | AC | 73 ms
71,160 KB |
testcase_02 | AC | 72 ms
71,352 KB |
testcase_03 | AC | 87 ms
75,808 KB |
testcase_04 | AC | 81 ms
75,808 KB |
testcase_05 | AC | 81 ms
75,792 KB |
testcase_06 | AC | 82 ms
75,988 KB |
testcase_07 | AC | 82 ms
75,960 KB |
testcase_08 | AC | 82 ms
75,972 KB |
testcase_09 | AC | 81 ms
75,900 KB |
testcase_10 | AC | 81 ms
75,864 KB |
testcase_11 | AC | 80 ms
75,796 KB |
testcase_12 | AC | 82 ms
75,812 KB |
testcase_13 | AC | 186 ms
79,128 KB |
testcase_14 | AC | 189 ms
79,856 KB |
testcase_15 | AC | 186 ms
79,156 KB |
testcase_16 | AC | 181 ms
79,356 KB |
testcase_17 | AC | 180 ms
79,292 KB |
testcase_18 | AC | 185 ms
80,208 KB |
testcase_19 | AC | 187 ms
79,140 KB |
testcase_20 | AC | 197 ms
79,908 KB |
testcase_21 | AC | 182 ms
79,212 KB |
testcase_22 | AC | 181 ms
79,148 KB |
testcase_23 | AC | 1,192 ms
112,580 KB |
testcase_24 | AC | 1,202 ms
112,880 KB |
testcase_25 | AC | 1,214 ms
112,368 KB |
testcase_26 | AC | 1,237 ms
112,452 KB |
testcase_27 | AC | 1,225 ms
112,384 KB |
testcase_28 | AC | 521 ms
108,252 KB |
testcase_29 | AC | 605 ms
108,120 KB |
testcase_30 | AC | 869 ms
112,448 KB |
testcase_31 | AC | 877 ms
112,524 KB |
testcase_32 | AC | 883 ms
112,528 KB |
ソースコード
class SegmentTree(): def __init__(self, arr, func=min, ie=2**63): self.h = (len(arr) - 1).bit_length() self.n = 2**self.h self.ie = ie self.func = func self.tree = [ie for _ in range(2 * self.n)] for i in range(len(arr)): self.tree[self.n + i] = arr[i] for i in range(1, self.n)[::-1]: self.tree[i] = func(self.tree[2 * i], self.tree[2 * i + 1]) def set(self, idx, x): idx += self.n self.tree[idx] = x while idx: idx >>= 1 self.tree[idx] = self.func(self.tree[2 * idx], self.tree[2 * idx + 1]) def query(self, lt, rt): lt += self.n rt += self.n vl = vr = self.ie while rt - lt > 0: if lt & 1: vl = self.func(vl, self.tree[lt]) lt += 1 if rt & 1: rt -= 1 vr = self.func(self.tree[rt], vr) lt >>= 1 rt >>= 1 return self.func(vl, vr) INF = 10**18 N = int(input()) A = list(map(int, input().split())) S = sorted([(A[i], i) for i in range(N)]) res = INF st1 = SegmentTree([INF] * N, min, INF) for i in range(N): a, idx = S[i] st1.set(idx, a) lt = st1.query(0, idx) rt = st1.query(idx + 1, N) if lt == INF or rt == INF: continue res = min(res, lt + rt + a) st2 = SegmentTree([INF] * N, min, INF) for i in range(N)[::-1]: a, idx = S[i] st2.set(idx, a) lt = st2.query(0, idx) rt = st2.query(idx + 1, N) if lt == INF or rt == INF: continue res = min(res, lt + rt + a) print(res if res != INF else -1)