結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー 👑 KazunKazun
提出日時 2020-06-27 01:10:10
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,384 bytes
コンパイル時間 422 ms
コンパイル使用メモリ 86,912 KB
実行使用メモリ 84,052 KB
最終ジャッジ日時 2023-09-18 13:26:25
合計ジャッジ時間 5,578 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
76,136 KB
testcase_01 AC 87 ms
71,572 KB
testcase_02 AC 86 ms
71,580 KB
testcase_03 AC 88 ms
71,360 KB
testcase_04 AC 90 ms
71,744 KB
testcase_05 AC 88 ms
71,756 KB
testcase_06 AC 88 ms
71,652 KB
testcase_07 AC 89 ms
71,652 KB
testcase_08 AC 89 ms
71,596 KB
testcase_09 AC 89 ms
71,500 KB
testcase_10 AC 88 ms
71,648 KB
testcase_11 AC 87 ms
71,408 KB
testcase_12 AC 88 ms
71,828 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

def Binary_Search_Count(A,x,sort=True,equal=False):
    """2分探索によって,x未満の要素の個数を調べる.

    A:リスト
    x:調べる要素
    sort:ソートをする必要があるかどうか(Trueで必要)
    equal:Trueのときはx"未満"がx"以下"になる
    """
    if sort:
        A.sort()
        
    N=len(A)
    if A[-1]<=x:
        return N
    elif x<A[0] or (not equal and x==A[0]):
        return 0

    L,R=0,N
    while R-L>1:
        C=L+(R-L)//2
        if x<A[C] or (not equal and x==A[C]):
            R=C
        else:
            L=C
    return R

def Binary_Search_Low_Value(A,x,sort=True,equal=False):
    """Aのx未満の要素の中で最大のものを出力する.

    A:リスト
    x:調べる要素
    sort:ソートをする必要があるかどうか(Trueで必要)
    equal:Trueのときはx"未満"がx"以下"になる
    ※全ての要素がx以上(超える)場合はNoneが返される.
    """
    
    if sort:
        A.sort()

    K=Binary_Search_Count(A,x,sort=False,equal=equal)
    if K==0:
        return None
    else:
        return A[K-1]

def Binary_Search_High_Value(A,x,sort=True,equal=False):
    """Aのxを超える要素の中で最大のものを出力する.

    A:リスト
    x:調べる要素
    sort:ソートをする必要があるかどうか(Trueで必要)
    equal:Trueのときはx"を超える"がx"以上"になる
    ※全ての要素がx以下(未満)場合はNoneが返される.
    """
    
    if sort:
        A.sort()

    K=Binary_Search_Count(A,x,sort=False,equal=not equal)
    if K==len(A):
        return None
    else:
        return A[K]
#----------------------------------------------------
from copy import copy
import sys
N=int(input())
A=list(map(int,input().split()))

B=copy(A)
B.sort()
if B==A or B[::-1]==A:
    print(-1)
    sys.exit()

#Type A
M=10**40
Lmin=[0]*N
for i in range(N):
    M=Lmin[i]=min(M,A[i])

M=10**40
Rmin=[0]*N
for i in range(N-1,-1,-1):
    M=Rmin[i]=min(M,A[i])

alpha=10**40
for i in range(1,N-1):
    if Lmin[i-1]<A[i] and A[i]>Rmin[i+1]:
        alpha=min(alpha,Lmin[i-1]+A[i]+Rmin[i+1])

beta=10**40
for i in range(1,N-1):
    a=A[i]
    P=Binary_Search_High_Value(A[:i],a,equal=False)
    Q=Binary_Search_High_Value(A[i+1:],a,equal=False)
    if P!=None and Q!=None:
        beta=min(beta,P+Q+a)

print(min(alpha,beta))
0