結果
| 問題 |
No.2078 魔抜けなパープル
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-09-26 19:33:18 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 537 ms / 2,000 ms |
| コード長 | 1,057 bytes |
| コンパイル時間 | 165 ms |
| コンパイル使用メモリ | 82,428 KB |
| 実行使用メモリ | 140,092 KB |
| 最終ジャッジ日時 | 2024-12-22 15:39:53 |
| 合計ジャッジ時間 | 3,238 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 7 |
ソースコード
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())))