結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー titiatitia
提出日時 2020-06-26 22:37:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 701 ms / 2,000 ms
コード長 2,381 bytes
コンパイル時間 324 ms
コンパイル使用メモリ 87,284 KB
実行使用メモリ 204,256 KB
最終ジャッジ日時 2023-09-18 06:08:10
合計ジャッジ時間 10,361 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
71,272 KB
testcase_01 AC 77 ms
71,340 KB
testcase_02 AC 76 ms
71,296 KB
testcase_03 AC 77 ms
71,588 KB
testcase_04 AC 79 ms
71,336 KB
testcase_05 AC 78 ms
71,460 KB
testcase_06 AC 78 ms
71,260 KB
testcase_07 AC 77 ms
71,388 KB
testcase_08 AC 78 ms
71,412 KB
testcase_09 AC 78 ms
71,296 KB
testcase_10 AC 75 ms
71,464 KB
testcase_11 AC 78 ms
71,352 KB
testcase_12 AC 78 ms
71,404 KB
testcase_13 AC 143 ms
79,504 KB
testcase_14 AC 141 ms
79,464 KB
testcase_15 AC 141 ms
79,512 KB
testcase_16 AC 142 ms
79,400 KB
testcase_17 AC 145 ms
79,592 KB
testcase_18 AC 148 ms
79,412 KB
testcase_19 AC 144 ms
79,552 KB
testcase_20 AC 142 ms
79,800 KB
testcase_21 AC 145 ms
79,476 KB
testcase_22 AC 147 ms
79,644 KB
testcase_23 AC 692 ms
203,864 KB
testcase_24 AC 701 ms
203,736 KB
testcase_25 AC 696 ms
204,256 KB
testcase_26 AC 691 ms
203,964 KB
testcase_27 AC 686 ms
203,920 KB
testcase_28 AC 471 ms
202,416 KB
testcase_29 AC 473 ms
202,144 KB
testcase_30 AC 577 ms
203,132 KB
testcase_31 AC 566 ms
203,108 KB
testcase_32 AC 567 ms
203,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

MINL=[]
X=10**9
for a in A:
    X=min(a,X)
    MINL.append(X)

X=10**9
MINR=[]
for a in A[::-1]:
    X=min(a,X)
    MINR.append(X)

MINR=MINR[::-1]

ANS=3*10**9

for i in range(1,N-1):
    if A[i]>MINL[i-1] and A[i]>MINR[i+1]:
        ANS=min(ANS,A[i]+MINL[i-1]+MINR[i+1])


SA=sorted(set(A))
compression_dict={a: ind for ind, a in enumerate(SA)}
A=[compression_dict[a] for a in A] # [2, 3, 0, 1, 4]

LEN=N+2 # 必要なら座標圧縮する

BIT=[0]*(LEN+1) # 1-indexedなtree

def update(v,w): # index vにwを加える
    while v<=LEN:
        BIT[v]+=w
        v+=(v&(-v)) # 自分を含む大きなノードへ. たとえばv=3→v=4

def getvalue(v): # [1,v]の区間の和を求める
    ANS=0
    while v!=0:
        ANS+=BIT[v]
        v-=(v&(-v)) # 自分より小さい2ベキのノードへ. たとえばv=3→v=2へ
    return ANS

def bisect_on_BIT(x): # [1,ind]の和がはじめてx以上になるindexを探す

    if x<=0:
        return 0
    
    ANS=0
    h=1<<(LEN.bit_length()-1) # LEN以下の最小の2ベキ
    while h>0:
        if ANS+h<=LEN and BIT[ANS+h]<x:
            x-=BIT[ANS+h]
            ANS+=h
        h//=2

    return ANS+1 # LENまでの和がx未満のとき, LEN+1を返すことに注意

for a in A:
    update(a+1,1)

BITL=[0]*(LEN+1) # 1-indexedなtree

def updateL(v,w): # index vにwを加える
    while v<=LEN:
        BITL[v]+=w
        v+=(v&(-v)) # 自分を含む大きなノードへ. たとえばv=3→v=4

def getvalueL(v): # [1,v]の区間の和を求める
    ANS=0
    while v!=0:
        ANS+=BITL[v]
        v-=(v&(-v)) # 自分より小さい2ベキのノードへ. たとえばv=3→v=2へ
    return ANS

def bisect_on_BITL(x): # [1,ind]の和がはじめてx以上になるindexを探す

    if x<=0:
        return 0
    
    ANS=0
    h=1<<(LEN.bit_length()-1) # LEN以下の最小の2ベキ
    while h>0:
        if ANS+h<=LEN and BITL[ANS+h]<x:
            x-=BITL[ANS+h]
            ANS+=h
        h//=2

    return ANS+1 # LENまでの和がx未満のとき, LEN+1を返すことに注意

for a in A:
    x=bisect_on_BIT(getvalue(a+1)+1)
    y=bisect_on_BITL(getvalueL(a+1)+1)

    if x!=LEN+1 and y!=LEN+1:
        ANS=min(ANS,SA[x-1]+SA[y-1]+SA[a])

    update(a+1,-1)
    updateL(a+1,1)

if ANS==3*10**9:
    print(-1)
else:
    print(ANS)
    

0