結果

問題 No.913 木の燃やし方
ユーザー roaris
提出日時 2023-03-16 06:03:34
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,620 bytes
コンパイル時間 316 ms
コンパイル使用メモリ 81,408 KB
実行使用メモリ 120,636 KB
最終ジャッジ日時 2024-09-18 09:13:13
合計ジャッジ時間 28,174 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

class CHT:
    def __init__(self):
        self.deq = deque()
        
    def check(self, f1, f2, f3):
        return (f2[0] - f1[0]) * (f3[1] - f2[1]) >= (f2[1] - f1[1]) * (f3[0] - f2[0])
    
    def f(self, f1, x):
        return f1[0]*x + f1[1]
    
    # add f_i(x) = a*x + b
    def add_line(self, a, b):
        f1 = (a, b)
        while len(self.deq) >= 2 and self.check(self.deq[-2], self.deq[-1], f1):
            self.deq.pop()
        self.deq.append(f1)
    
    # min f_i(x)
    def get(self, x):
        while len(self.deq) >= 2 and self.f(self.deq[0], x) >= self.f(self.deq[1], x):
            self.deq.popleft()
        return self.f(self.deq[0], x)

def dfs(l, r): # [l, r)
    global ans
    
    if l>=r:
        return
    
    m = (l+r)//2
    dfs(l, m)
    dfs(m+1, r)
    
    # [l, m)
    if l<m:
        cht = CHT()
        
        for i in range(m+1, r+1):
            cht.add_line(-2*i, i*i+acc[i])
        
        res = 10**18
        
        for i in range(l, m):
            res = min(res, cht.get(i)+i*i-acc[i])
            ans[i] = min(ans[i], res)
    
    # [m, r)
    if m<r:
        cht = CHT()
        
        for i in range(l, m+1):
            cht.add_line(-2*i, i*i-acc[i])
        
        res = 10**18
        
        for i in range(r, m, -1):
            res = min(res, cht.get(i)+i*i+acc[i])
            ans[i-1] = min(ans[i-1], res)

N = int(input())
A = list(map(int, input().split()))
acc = [0]

for Ai in A:
    acc.append(acc[-1]+Ai)

ans = [10**18]*N
dfs(0, N)

for i in range(N):
    print(ans[i])
0