結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー toyuzukotoyuzuko
提出日時 2020-06-27 14:27:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,658 bytes
コンパイル時間 151 ms
コンパイル使用メモリ 10,812 KB
実行使用メモリ 48,408 KB
最終ジャッジ日時 2023-09-18 19:25:34
合計ジャッジ時間 8,472 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,096 KB
testcase_01 AC 16 ms
8,036 KB
testcase_02 AC 16 ms
8,140 KB
testcase_03 AC 17 ms
8,024 KB
testcase_04 AC 18 ms
7,956 KB
testcase_05 AC 18 ms
7,984 KB
testcase_06 AC 18 ms
8,036 KB
testcase_07 AC 17 ms
7,952 KB
testcase_08 AC 18 ms
8,132 KB
testcase_09 AC 18 ms
8,012 KB
testcase_10 AC 18 ms
7,964 KB
testcase_11 AC 18 ms
7,984 KB
testcase_12 AC 18 ms
8,128 KB
testcase_13 AC 344 ms
10,368 KB
testcase_14 AC 345 ms
10,416 KB
testcase_15 AC 347 ms
10,288 KB
testcase_16 AC 346 ms
10,248 KB
testcase_17 AC 346 ms
10,224 KB
testcase_18 AC 344 ms
10,404 KB
testcase_19 AC 348 ms
10,344 KB
testcase_20 AC 344 ms
10,388 KB
testcase_21 AC 344 ms
10,348 KB
testcase_22 AC 350 ms
10,372 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0