結果
| 問題 |
No.1095 Smallest Kadomatsu Subsequence
|
| コンテスト | |
| ユーザー |
titia
|
| 提出日時 | 2020-06-26 22:32:36 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,377 bytes |
| コンパイル時間 | 197 ms |
| コンパイル使用メモリ | 12,928 KB |
| 実行使用メモリ | 57,084 KB |
| 最終ジャッジ日時 | 2024-07-04 22:17:02 |
| 合計ジャッジ時間 | 6,474 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 6 WA * 14 TLE * 1 -- * 9 |
ソースコード
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)
titia