結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー titiatitia
提出日時 2020-06-26 22:32:36
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 2,377 bytes
コンパイル時間 418 ms
コンパイル使用メモリ 11,080 KB
実行使用メモリ 12,500 KB
最終ジャッジ日時 2023-09-18 05:58:12
合計ジャッジ時間 6,048 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
12,500 KB
testcase_01 AC 16 ms
8,124 KB
testcase_02 AC 16 ms
8,032 KB
testcase_03 AC 17 ms
8,108 KB
testcase_04 WA -
testcase_05 AC 17 ms
8,104 KB
testcase_06 WA -
testcase_07 AC 18 ms
8,112 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 146 ms
10,208 KB
testcase_14 AC 146 ms
10,380 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 148 ms
10,184 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

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]+a)

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

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

0