結果

問題 No.913 木の燃やし方
ユーザー vwxyzvwxyz
提出日時 2024-04-17 15:39:30
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,053 bytes
コンパイル時間 356 ms
コンパイル使用メモリ 82,008 KB
実行使用メモリ 135,880 KB
最終ジャッジ日時 2024-04-17 15:40:02
合計ジャッジ時間 26,179 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
54,272 KB
testcase_01 AC 45 ms
53,888 KB
testcase_02 AC 46 ms
54,400 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 712 ms
109,296 KB
testcase_14 AC 676 ms
107,568 KB
testcase_15 AC 665 ms
107,672 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 746 ms
112,016 KB
testcase_27 AC 494 ms
133,272 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

class Convex_Hull_Trick_deque:
    """
    f_i = a_ix + b_i とする。f_i の追加および、min_i f(x) の取得ができるデータ構造。
    ただし、傾き a_i は降順に追加されなければならない。
    また、クエリ x も昇順に実行されなければならない。
    """

    def __init__(self):
        self.lines=deque()

    def add_line(self,a,b):
        lines=self.lines
        while len(lines) >= 2:
            a1,b1=lines[-2]
            a2,b2=lines[-1]
            if (a2-a1)*(b-b2)<(b2-b1)*(a-a2):
                break
            lines.pop()
        lines.append((a, b))

    def __call__(self, x):
        lines=self.lines
        a,b=lines[0]
        y=a*x+b
        while len(lines) >= 2:
            a2, b2 = lines[1]
            y2 = a2 * x + b2
            if y < y2:
                break
            y = y2
            lines.popleft()
        return y

N=int(input())
A=list(map(int,input().split()))
C=[0]+A
for i in range(1,N+1):
    C[i]+=C[i-1]
inf=1<<60
ans_lst=[inf]*N
def solve(l,r):
    if r-l==1:
        ans_lst[l]=min(ans_lst[l],1+A[l])
    elif r-l==2:
        ans_lst[l]=min(ans_lst[l],1+A[l],4+A[l]+A[l+1])
        ans_lst[l]=min(ans_lst[l+1],1+A[l+1],4+A[l]+A[l+1])
    else:
        m=(l+r)//2
        mi=[inf]*(r-l)
        CHT_left=Convex_Hull_Trick_deque()
        for i in range(l,m+1):
            CHT_left.add_line(-2*i,i*i-C[i])
        for j in range(m+1,r+1):
            mi[j-1-l]=min(mi[j-1-l],CHT_left(j)+j*j+C[j])
        CHT_right=Convex_Hull_Trick_deque()
        for j in range(m+1,r+1):
            CHT_right.add_line(-2*j,j*j+C[j])
        for i in range(l,m+1):
            mi[i-l]=min(mi[i-l],CHT_right(i)+i*i-C[i])

        for i in range(l+1,m+1):
            mi[i-l]=min(mi[i-l],mi[i-l-1])
        for j in range(r-1,m,-1):
            mi[j-1-l]=min(mi[j-1-l],mi[j-l])
        for i in range(l,r):
            ans_lst[i]=min(ans_lst[i],mi[i-l])
        solve(l,m)
        solve(m+1,r)
solve(0,N)
print(*ans_lst,sep="\n")
0