結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー wattaiheiwattaihei
提出日時 2020-06-26 22:01:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,666 ms / 2,000 ms
コード長 3,583 bytes
コンパイル時間 446 ms
コンパイル使用メモリ 86,808 KB
実行使用メモリ 264,440 KB
最終ジャッジ日時 2023-09-18 04:37:47
合計ジャッジ時間 18,173 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,092 KB
testcase_01 AC 72 ms
71,532 KB
testcase_02 AC 71 ms
71,264 KB
testcase_03 AC 81 ms
75,808 KB
testcase_04 AC 82 ms
75,948 KB
testcase_05 AC 82 ms
75,908 KB
testcase_06 AC 81 ms
75,972 KB
testcase_07 AC 80 ms
75,856 KB
testcase_08 AC 83 ms
75,992 KB
testcase_09 AC 83 ms
75,976 KB
testcase_10 AC 82 ms
75,984 KB
testcase_11 AC 85 ms
75,936 KB
testcase_12 AC 83 ms
75,908 KB
testcase_13 AC 226 ms
82,732 KB
testcase_14 AC 176 ms
80,824 KB
testcase_15 AC 218 ms
82,132 KB
testcase_16 AC 222 ms
82,044 KB
testcase_17 AC 218 ms
81,376 KB
testcase_18 AC 217 ms
82,236 KB
testcase_19 AC 215 ms
80,760 KB
testcase_20 AC 215 ms
82,416 KB
testcase_21 AC 214 ms
81,028 KB
testcase_22 AC 185 ms
80,816 KB
testcase_23 AC 1,492 ms
263,544 KB
testcase_24 AC 1,666 ms
263,152 KB
testcase_25 AC 1,487 ms
264,440 KB
testcase_26 AC 1,497 ms
263,548 KB
testcase_27 AC 1,495 ms
263,328 KB
testcase_28 AC 866 ms
264,148 KB
testcase_29 AC 802 ms
264,004 KB
testcase_30 AC 1,208 ms
262,944 KB
testcase_31 AC 1,210 ms
262,868 KB
testcase_32 AC 1,201 ms
262,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from bisect import bisect_right, bisect_left
# instead of AVLTree
class BITbisect():
    def __init__(self, InputProbNumbers):
        # 座圧
        self.ind_to_co = [-10**18]
        self.co_to_ind = {}
        for ind, num in enumerate(sorted(list(set(InputProbNumbers)))):
            self.ind_to_co.append(num)
            self.co_to_ind[num] = ind+1
        self.max = len(self.co_to_ind)
        self.data = [0]*(self.max+1)
    
    def __str__(self):
        retList = []
        for i in range(1, self.max+1):
            x = self.ind_to_co[i]
            if self.count(x):
                c = self.count(x)
                for _ in range(c):
                    retList.append(x)
        return "[" + ", ".join([str(a) for a in retList]) + "]"
    
    def __getitem__(self, key):
        key += 1
        s = 0
        ind = 0
        l = self.max.bit_length()
        for i in reversed(range(l)):
            if ind + (1<<i) <= self.max:
                if s + self.data[ind+(1<<i)] < key:
                    s += self.data[ind+(1<<i)]
                    ind += (1<<i)
        if ind == self.max or key < 0:
            raise IndexError("BIT index out of range")
        return self.ind_to_co[ind+1]
    
    def __len__(self):
        return self._query_sum(self.max)
    
    def __contains__(self, num):
        if not num in self.co_to_ind:
            return False
        return self.count(num) > 0
    
    # 0からiまでの区間和
    # 左に進んでいく
    def _query_sum(self, i):
        s = 0
        while i > 0:
            s += self.data[i]
            i -= i & -i
        return s

    # i番目の要素にxを足す
    # 上に登っていく
    def _add(self, i, x):
        while i <= self.max:
            self.data[i] += x
            i += i & -i
    
    # 値xを挿入
    def push(self, x):
        if not x in self.co_to_ind:
            raise KeyError("The pushing number didnt initialized")
        self._add(self.co_to_ind[x], 1)
            
    # 値xを削除
    def delete(self, x):
        if not x in self.co_to_ind:
            raise KeyError("The deleting number didnt initialized")
        if self.count(x) <= 0:
            raise ValueError("The deleting number doesnt exist")
        self._add(self.co_to_ind[x], -1)

    # 要素xの個数
    def count(self, x):
        return self._query_sum(self.co_to_ind[x]) - self._query_sum(self.co_to_ind[x]-1)
    
    # 値xを超える最低ind
    def bisect_right(self, x):
        if x in self.co_to_ind:
            i = self.co_to_ind[x]
        else:
            i = bisect_right(self.ind_to_co, x) - 1
        return self._query_sum(i)

    # 値xを下回る最低ind
    def bisect_left(self, x):
        if x in self.co_to_ind:
            i = self.co_to_ind[x]
        else:
            i = bisect_left(self.ind_to_co, x)
        if i == 1:
            return 0
        return self._query_sum(i-1)

import sys
input = sys.stdin.readline

INF = 10**18

N = int(input())
A = list(map(int, input().split()))

Bit1 = BITbisect(A)
Bit2 = BITbisect(A)

ans = INF

for a in A:
    Bit2.push(a)

for i, a in enumerate(A):
    Bit2.delete(a)
    if i == 0 or i == N-1:
        Bit1.push(a)
        continue

    c1 = Bit1[0]
    c2 = Bit2[0]
    if a > c1 and a > c2:
        ans = min(ans, a+c1+c2)

    ind1 = Bit1.bisect_left(a)
    ind2 = Bit2.bisect_left(a)

    if ind1 < len(Bit1) and ind2 < len(Bit2):
        b1 = Bit1[ind1]
        b2 = Bit2[ind2]
        ans = min(ans, a + b1 + b2)
    
    Bit1.push(a)

if ans == INF:
    print(-1)
else:
    print(ans)
0