Loading

AcWing 301. 任务安排2

题目链接

https://www.acwing.com/problem/content/303/

题解

AC代码

import java.util.*;

public class Main {
    static int N = (int) 3e5 + 10;
    static long[] sumT = new long[N], sumC = new long[N];
    static long[] f = new long[N];
    static int[] q = new int[N];
    static int n, s;

    public static void main(String[] args)  {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        s = sc.nextInt();

        for (int i = 1; i <= n; i ++) {
            sumT[i] = sumT[i - 1] + sc.nextInt();
            sumC[i] = sumC[i - 1] + sc.nextInt();
        }

        int hh = 0, tt = 0;
        q[0] = 0;

        for (int i = 1; i <= n; i ++) {
            while (hh < tt && (f[q[hh + 1]] - f[q[hh]]) <= (s + sumT[i]) * (sumC[q[hh + 1]] - sumC[q[hh]])) hh ++;
            int j = q[hh];
            f[i] = f[j] - sumC[j] * (s + sumT[i]) + s * sumC[n] + sumT[i] * sumC[i];
            while (hh < tt && (double) (f[q[tt]] - f[q[tt - 1]]) * (sumC[i] - sumC[q[tt]]) 
                                >= 
                              (double) (f[i] - f[q[tt]]) * (sumC[q[tt]] - sumC[q[tt - 1]])) tt --;
            q[++ tt] = i;
        }

        System.out.println(f[n]);
    }
}
posted @ 2022-06-18 18:12  Doubest  阅读(25)  评论(0编辑  收藏  举报