목차

Batch Scheduling

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()