結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー tonnnura172tonnnura172
提出日時 2020-06-27 20:27:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 393 ms / 2,000 ms
コード長 2,740 bytes
コンパイル時間 509 ms
コンパイル使用メモリ 86,744 KB
実行使用メモリ 121,236 KB
最終ジャッジ日時 2023-09-19 09:19:01
合計ジャッジ時間 10,540 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 165 ms
80,080 KB
testcase_01 AC 164 ms
80,252 KB
testcase_02 AC 167 ms
80,080 KB
testcase_03 AC 166 ms
80,712 KB
testcase_04 AC 166 ms
80,676 KB
testcase_05 AC 167 ms
80,628 KB
testcase_06 AC 165 ms
80,908 KB
testcase_07 AC 164 ms
80,712 KB
testcase_08 AC 166 ms
80,864 KB
testcase_09 AC 165 ms
80,676 KB
testcase_10 AC 165 ms
80,680 KB
testcase_11 AC 163 ms
80,892 KB
testcase_12 AC 162 ms
80,760 KB
testcase_13 AC 234 ms
83,036 KB
testcase_14 AC 209 ms
82,920 KB
testcase_15 AC 213 ms
83,332 KB
testcase_16 AC 212 ms
82,892 KB
testcase_17 AC 210 ms
82,992 KB
testcase_18 AC 214 ms
83,780 KB
testcase_19 AC 217 ms
83,548 KB
testcase_20 AC 223 ms
83,372 KB
testcase_21 AC 214 ms
83,576 KB
testcase_22 AC 205 ms
82,856 KB
testcase_23 AC 393 ms
120,984 KB
testcase_24 AC 380 ms
120,796 KB
testcase_25 AC 383 ms
120,820 KB
testcase_26 AC 387 ms
121,236 KB
testcase_27 AC 381 ms
120,832 KB
testcase_28 AC 359 ms
121,028 KB
testcase_29 AC 365 ms
120,876 KB
testcase_30 AC 381 ms
120,736 KB
testcase_31 AC 376 ms
120,912 KB
testcase_32 AC 372 ms
120,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd, log2
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul, add
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

class SegmentTree:
    """Segment Tree (Point Update & Range Query)
    Query
        1. update(i, val): update i-th value(0-indexed) to val
        2. query(low, high): find f(value) in [low, high)
    Complexity
        time complexity: O(log n)
        space complexity: O(n)
    """
    def __init__(self, N, f, default):
        self.N = 1 << (N-1).bit_length()
        self.default = default
        self.f = f
        self.segtree = [self.default] * ((self.N << 1) - 1)
    @classmethod
    def create_from_array(cls, arr, f, default):
        N = len(arr)
        self = cls(N, f, default)
        for i in range(N):
            self.segtree[self.N - 1 + i] = arr[i]
        for i in reversed(range(self.N - 1)):
            self.segtree[i] = self.f(
                self.segtree[(i << 1) + 1], self.segtree[(i << 1) + 2])
        return self
    def update(self, i, val):  # update
        i += self.N - 1
        self.segtree[i] = val
        while i > 0:
            i = (i - 1) >> 1
            self.segtree[i] = self.f(
                self.segtree[(i << 1)+1], self.segtree[(i << 1)+2])
    def query(self, low, high):  # query
        # query [l, r)
        low, high = low + self.N, high + self.N
        ret = self.default
        while low < high:
            if low & 1:
                ret = self.f(ret, self.segtree[low-1])
                low += 1
            if high & 1:
                high -= 1
                ret = self.f(ret, self.segtree[high-1])
            low, high = low >> 1, high >> 1
        return ret
    def get(self, k):  # get k-th value(0-indexed)
        return self.segtree[k+self.N-1]
    def all(self):  # all range query
        return self.segtree[0]

N = INT()
A = LIST()

segtree = SegmentTree.create_from_array(A, min, 10**9)

ans = 10**10
for i in range(1, N-1):
    l = segtree.query(0, i)
    r = segtree.query(i+1, N)
    if l < A[i] > r or l > A[i] < r:
        ans = min(ans, A[i]+l+r)
    
print(ans if ans != 10**10 else -1)
0