結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー tonnnura172tonnnura172
提出日時 2020-06-27 20:27:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 308 ms / 2,000 ms
コード長 2,740 bytes
コンパイル時間 287 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 117,728 KB
最終ジャッジ日時 2024-07-05 21:03:13
合計ジャッジ時間 6,873 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
65,792 KB
testcase_01 AC 75 ms
65,792 KB
testcase_02 AC 71 ms
65,408 KB
testcase_03 AC 75 ms
66,688 KB
testcase_04 AC 75 ms
66,688 KB
testcase_05 AC 73 ms
66,816 KB
testcase_06 AC 73 ms
66,816 KB
testcase_07 AC 74 ms
66,944 KB
testcase_08 AC 74 ms
66,432 KB
testcase_09 AC 73 ms
66,816 KB
testcase_10 AC 72 ms
67,072 KB
testcase_11 AC 74 ms
66,688 KB
testcase_12 AC 75 ms
67,072 KB
testcase_13 AC 133 ms
79,232 KB
testcase_14 AC 122 ms
78,720 KB
testcase_15 AC 132 ms
79,360 KB
testcase_16 AC 134 ms
78,720 KB
testcase_17 AC 132 ms
79,360 KB
testcase_18 AC 131 ms
79,616 KB
testcase_19 AC 133 ms
79,360 KB
testcase_20 AC 131 ms
79,104 KB
testcase_21 AC 132 ms
79,232 KB
testcase_22 AC 123 ms
78,720 KB
testcase_23 AC 308 ms
117,396 KB
testcase_24 AC 306 ms
117,404 KB
testcase_25 AC 301 ms
117,672 KB
testcase_26 AC 308 ms
117,408 KB
testcase_27 AC 302 ms
117,412 KB
testcase_28 AC 272 ms
117,680 KB
testcase_29 AC 286 ms
117,728 KB
testcase_30 AC 296 ms
117,164 KB
testcase_31 AC 293 ms
117,404 KB
testcase_32 AC 294 ms
117,152 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