結果

問題 No.2078 魔抜けなパープル
ユーザー 👑 rin204rin204
提出日時 2022-09-26 19:33:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 560 ms / 2,000 ms
コード長 1,057 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 86,944 KB
実行使用メモリ 140,932 KB
最終ジャッジ日時 2023-08-24 03:00:13
合計ジャッジ時間 3,744 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
80,664 KB
testcase_01 AC 390 ms
118,008 KB
testcase_02 AC 336 ms
105,164 KB
testcase_03 AC 373 ms
111,484 KB
testcase_04 AC 344 ms
103,456 KB
testcase_05 AC 327 ms
103,540 KB
testcase_06 AC 560 ms
140,932 KB
testcase_07 AC 241 ms
102,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

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 solve(c, n):    
    n += 1
    H = [i for i in range(n)]
    dp = [0] * n
    cht = CHT()
    cht.add_line(-2 * H[0], H[0] ** 2)
    for i, h in enumerate(H[1:], 1):
        dp[i] = h ** 2 + c + cht.get(h)
        cht.add_line(-2 * h, dp[i] + h ** 2)
    return dp[-1]

for _ in range(int(input())):
    print(solve(*map(int, input().split())))
0