1. Title link:
Task scheduling 2
2. The main idea of the topic:
Chinese question~~
3. Analysis (drawing):
嘤嘤嘤, the first slope to optimize DP.
There are a lot of explanations on the Internet, and the big guys are also very good. I will not make wheels.
4. Code implementation:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int M = (int)3e5;
const int inf = 0x3f3f3f3f;
ll sumt[M + 5];
ll sumc[M + 5];
ll q[M + 5];
ll f[M + 5];
int main()
{
int n, s;
scanf("%d %d", &n, &s);
for(int i = 1; i <= n; ++i)
{
scanf("%lld %lld", &sumt[i], &sumc[i]);
sumt[i] += sumt[i - 1];
sumc[i] += sumc[i - 1];
}
int l = 1, r = 0;
q[++r] = 0;
for(int i = 1; i <= n; ++i)
{
while(l < r && (f[q[l + 1]] - f[q[l]]) <= (s + sumt[i]) * (sumc[q[l + 1]] - sumc[q[l]])) ++l;
if(l <= r) f[i] = f[q[l]] + sumt[i] * (sumc[i] - sumc[q[l]]) + s * (sumc[n] - sumc[q[l]]);
while(l < r && (f[i] - f[q[r]]) * (sumc[q[r]] - sumc[q[r - 1]]) <= (f[q[r]] - f[q[r - 1]]) * (sumc[i] - sumc[q[r]])) --r;
q[++r] = i;
}
printf("%lld\n", f[n]);
return 0;
}