結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー 👑 KazunKazun
提出日時 2020-11-28 15:31:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,336 ms / 2,000 ms
コード長 3,553 bytes
コンパイル時間 360 ms
コンパイル使用メモリ 87,316 KB
実行使用メモリ 181,204 KB
最終ジャッジ日時 2023-10-10 00:32:28
合計ジャッジ時間 15,911 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
71,512 KB
testcase_01 AC 80 ms
71,272 KB
testcase_02 AC 78 ms
71,428 KB
testcase_03 AC 90 ms
75,960 KB
testcase_04 AC 89 ms
76,016 KB
testcase_05 AC 88 ms
76,140 KB
testcase_06 AC 89 ms
75,984 KB
testcase_07 AC 89 ms
75,844 KB
testcase_08 AC 87 ms
75,800 KB
testcase_09 AC 89 ms
76,100 KB
testcase_10 AC 91 ms
76,136 KB
testcase_11 AC 85 ms
75,852 KB
testcase_12 AC 86 ms
75,736 KB
testcase_13 AC 245 ms
81,608 KB
testcase_14 AC 269 ms
83,644 KB
testcase_15 AC 240 ms
80,344 KB
testcase_16 AC 259 ms
82,220 KB
testcase_17 AC 286 ms
82,712 KB
testcase_18 AC 265 ms
82,372 KB
testcase_19 AC 282 ms
83,504 KB
testcase_20 AC 267 ms
81,552 KB
testcase_21 AC 260 ms
81,456 KB
testcase_22 AC 294 ms
82,600 KB
testcase_23 AC 1,192 ms
180,200 KB
testcase_24 AC 1,336 ms
180,724 KB
testcase_25 AC 977 ms
181,204 KB
testcase_26 AC 1,150 ms
181,164 KB
testcase_27 AC 1,168 ms
180,716 KB
testcase_28 AC 687 ms
179,428 KB
testcase_29 AC 718 ms
179,488 KB
testcase_30 AC 866 ms
179,824 KB
testcase_31 AC 862 ms
180,264 KB
testcase_32 AC 870 ms
180,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Segment_Tree():
    """
    このプログラム内は1-index
    """

    def __init__(self,L,calc,unit):
        """calcを演算とするリストLのSegment Treeを作成

        calc:演算(2変数関数,モノイド)
        unit:モノイドcalcの単位元 (xe=ex=xを満たすe)
        """
        self.calc=calc
        self.unit=unit

        N=len(L)
        d=max(1,(N-1).bit_length())
        k=1<<d

        self.data=[unit]*k+L+[unit]*(k-len(L))
        self.N=k
        self.depth=d

        for i in range(k-1,0,-1):
            self.data[i]=calc(self.data[i<<1],self.data[i<<1|1])

    def get(self,k,index=1):
        """第k要素を取得
        """
        assert 0<=k-index<self.N,"添字が範囲外"
        return self.data[k-index+self.N]

    def update(self,k,x,index=1):
        """第k要素をxに変え,更新を行う.

        k:数列の要素
        x:更新後の値
        """
        assert 0<=k-index<self.N,"添字が範囲外"
        m=(k-index)+self.N
        self.data[m]=x

        while m>1:
            m>>=1
            self.data[m]=self.calc(self.data[m<<1],self.data[m<<1|1])


    def product(self,From,To,index=1,left_closed=True,right_closed=True):
        L=(From-index)+self.N+(not left_closed)
        R=(To-index)+self.N+(right_closed)

        vL=self.unit
        vR=self.unit

        while L<R:
            if L&1:
                vL=self.calc(vL,self.data[L])
                L+=1

            if R&1:
                R-=1
                vR=self.calc(self.data[R],vR)

            L>>=1
            R>>=1

        return self.calc(vL,vR)

    def all_product(self):
        return self.data[1]

    def max_right(self,l,r,cond,index=0):
        """以下の2つをともに満たすxの1つを返す.\n
        (1) r=l or cond(data[l]*data[l+1]*...*d[r-1]):True
        (2) r=x or cond(data[l]*data[l+1]*...*data[r]):False
        ※fが単調減少の時,cond(data[l]*...*data[r-1])を満たす最大のrとなる.

        cond:関数(引数が同じならば結果も同じ)
        cond(unit):True
        0<=l<=r<=n
        """
        l-=index
        assert 0<=l<=r<=self.num,"添字が範囲外"
        assert cond(self.unit),"単位元が条件を満たさない."

        if l==r:
            return r+index

        l+=(self.num-1)
        sm=self.unit

        calc=self.calc
        while True:
            while l%2:
                l=(l-1)>>1

            if not cond(calc(sm,self.data[l])):
                while l<self.num-1:
                    l=2*l+1

                    if cond(calc(sm,self.data[l])):
                        sm=calc(sm,self.data[l])
                        l+=1

                return min(l-(self.num-1)+index,r)

            sm=calc(sm,self.data[l])
            l+=1

            m=l+1
            if not (m&(-m) !=m):
                break

        return r+index
#================================================
import sys
input=sys.stdin.readline
N=int(input())
A=list(map(int,input().split()))
B=sorted(A)

inf=float("inf")
D={x:i for i,x in enumerate(B)}

S=Segment_Tree([inf]*N,min, inf)
T=Segment_Tree(B.copy(),min, inf)

X=inf
for i in range(N):
    m=D[A[i]]
    T.update(m, inf,0)

    if m>0:
        x=S.product(0,m-1,0)
        y=T.product(0,m-1,0)

        if x<A[i] and y<A[i]:
            X=min(X,x+y+A[i])

    if m<N-1:
        x=S.product(m+1,N-1,0)
        y=T.product(m+1,N-1,0)

        if x>A[i] and y>A[i]:
            X=min(X,x+y+A[i])

    S.update(m,A[i],0)

if X==inf:
    print(-1)
else:
    print(X)
0