ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 5498 |
문제명 | Batch Scheduling |
레벨 | 플래티넘 3 |
분류 |
DP, CHT |
시간복잡도 | O(n) |
인풋사이즈 | n<=10000 |
사용한 언어 | Python 3.11 |
제출기록 | 34464KB / 80ms |
최고기록 | 80ms |
해결날짜 | 2023/01/29 |
"""Solution code for "BOJ 5498. Batch Scheduling".
- Problem link: https://www.acmicpc.net/problem/5498
- Solution link: http://www.teferi.net/ps/problems/boj/5498
Tags: [DP] [CHT]
"""
import sys
from teflib import convexhulltrick
def main():
N = int(sys.stdin.readline())
S = int(sys.stdin.readline())
T_and_F = [[int(x) for x in sys.stdin.readline().split()] for _ in range(N)]
f_sum = [v := 0] + [v := v + f for t, f in T_and_F]
t_sum = [v := 0] + [v := v + t for t, f in T_and_F]
c = f_sum[-1] * S
dp = [None] * (N + 1)
dp[0] = t_sum[-1] * f_sum[-1]
dp = convexhulltrick.dp_with_cht(
cht=convexhulltrick.ConvexHullTrickForIncreasingQueries(),
dp=dp,
f_j=lambda j: t_sum[N - j],
f_i=lambda i: -f_sum[N - i],
g_j=lambda j: dp[j],
g_i=lambda i: f_sum[N - i] * (t_sum[N - i] - S) + c,
)
print(dp[-1])
if __name__ == '__main__':
main()