結果
問題 | No.1095 Smallest Kadomatsu Subsequence |
ユーザー |
![]() |
提出日時 | 2023-01-22 17:11:04 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,082 ms / 2,000 ms |
コード長 | 1,321 bytes |
コンパイル時間 | 345 ms |
コンパイル使用メモリ | 82,272 KB |
実行使用メモリ | 110,588 KB |
最終ジャッジ日時 | 2024-06-24 21:29:39 |
合計ジャッジ時間 | 12,566 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
class SegmentTree:def __init__(self, size, f=min, default=10 ** 18):self.size = 2**(size-1).bit_length()self.default = defaultself.dat = [default]*(self.size*2)self.f = fdef update(self, i, x):i += self.sizeself.dat[i] = xwhile i > 0:i >>= 1self.dat[i] = self.f(self.dat[i*2], self.dat[i*2+1])def query(self, l, r):l += self.sizer += self.sizelres, rres = self.default, self.defaultwhile l < r:if l & 1:lres = self.f(lres, self.dat[l])l += 1if r & 1:r -= 1rres = self.f(self.dat[r], rres)l >>= 1r >>= 1res = self.f(lres, rres)return resN = int(input())A = list(map(int, input().split()))B = [0] * Nfor i in range(N):B[i] = (A[i], i + 1)B.sort(reverse=True)inf = 10 ** 18T = SegmentTree(N + 2)ans = inffor a, i in B:x = T.query(0, i)y = T.query(i + 1, N + 2)ans = min(ans, x + a + y)T.update(i, a)for i, a in enumerate(A):i += 1x = T.query(0, i)y = T.query(i + 1, N + 2)if x > a or y > a:continueans = min(ans, x + a + y)print(ans) if ans < inf else print(-1)