結果
問題 | No.1095 Smallest Kadomatsu Subsequence |
ユーザー |
![]() |
提出日時 | 2020-06-26 22:37:21 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 596 ms / 2,000 ms |
コード長 | 2,381 bytes |
コンパイル時間 | 334 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 202,940 KB |
最終ジャッジ日時 | 2024-07-04 22:24:41 |
合計ジャッジ時間 | 8,744 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
N=int(input())A=list(map(int,input().split()))MINL=[]X=10**9for a in A:X=min(a,X)MINL.append(X)X=10**9MINR=[]for a in A[::-1]:X=min(a,X)MINR.append(X)MINR=MINR[::-1]ANS=3*10**9for 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なtreedef update(v,w): # index vにwを加えるwhile v<=LEN:BIT[v]+=wv+=(v&(-v)) # 自分を含む大きなノードへ. たとえばv=3→v=4def getvalue(v): # [1,v]の区間の和を求めるANS=0while v!=0:ANS+=BIT[v]v-=(v&(-v)) # 自分より小さい2ベキのノードへ. たとえばv=3→v=2へreturn ANSdef bisect_on_BIT(x): # [1,ind]の和がはじめてx以上になるindexを探すif x<=0:return 0ANS=0h=1<<(LEN.bit_length()-1) # LEN以下の最小の2ベキwhile h>0:if ANS+h<=LEN and BIT[ANS+h]<x:x-=BIT[ANS+h]ANS+=hh//=2return ANS+1 # LENまでの和がx未満のとき, LEN+1を返すことに注意for a in A:update(a+1,1)BITL=[0]*(LEN+1) # 1-indexedなtreedef updateL(v,w): # index vにwを加えるwhile v<=LEN:BITL[v]+=wv+=(v&(-v)) # 自分を含む大きなノードへ. たとえばv=3→v=4def getvalueL(v): # [1,v]の区間の和を求めるANS=0while v!=0:ANS+=BITL[v]v-=(v&(-v)) # 自分より小さい2ベキのノードへ. たとえばv=3→v=2へreturn ANSdef bisect_on_BITL(x): # [1,ind]の和がはじめてx以上になるindexを探すif x<=0:return 0ANS=0h=1<<(LEN.bit_length()-1) # LEN以下の最小の2ベキwhile h>0:if ANS+h<=LEN and BITL[ANS+h]<x:x-=BITL[ANS+h]ANS+=hh//=2return 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)