1 专题说明
本专题用来记录使用斜率优化的DP问题。
2 训练
题目1:300任务安排1
C++代码如下,
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, s;
cin >> n >> s;
vector<int> sumc(n + 10, 0), sumt(n + 10, 0);
vector<long long> f(n + 10, 0);
for (int i = 1; i <= n; ++i) {
int t, c;
cin >> t >> c;
sumt[i] = sumt[i-1] + t;
sumc[i] = sumc[i-1] + c;
}
//状态定义f[i]:将前i个任务处理完的所有方案的集合,属性为代价最小。
//状态初始化
for (int i = 0; i < f.size(); ++i) {
f[i] = 0x3f3f3f3f3f3f3f3f; //long long是8个字节
}
f[0] = 0;
//状态转移
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
f[i] = min(f[i], f[j] + (long long)sumt[i] * (sumc[i] - sumc[j]) + (long long)s * (sumc[n] - sumc[j]));
}
}
//最终答案
cout << f[n] << endl;
return 0;
}
题目2:301任务安排2
C++代码如下,
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 300010;
int n, s;
LL c[N], t[N];
LL f[N];
int q[N];
int main() {
scanf("%d%d", &n, &s);
for (int i = 1; i <= n; ++i) {
scanf("%lld%lld", &t[i], &c[i]);
t[i] += t[i-1];
c[i] += c[i-1];
}
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]]) <= (t[i] + s) * (c[q[hh+1]] - c[q[hh]])) hh++;
int j = q[hh];
f[i] = f[j] - (t[i] + s) * c[j] + t[i] * c[i] + s * c[n];
while (hh < tt && (__int128)(f[q[tt]] - f[q[tt-1]]) * (c[i] - c[q[tt-1]]) >= (__int128)(f[i] - f[q[tt-1]]) * (c[q[tt]] - c[q[tt-1]])) tt--;
q[++tt] = i;
}
printf("%lld\n", f[n]);
return 0;
}
题目3:302任务安排3
C++代码如下,
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 300010;
int n, s;
LL t[N], c[N];
LL f[N];
int q[N];
int main() {
scanf("%d%d", &n, &s);
for (int i = 1; i <= n; ++i) {
scanf("%lld%lld", &t[i], &c[i]);
t[i] += t[i-1];
c[i] += c[i-1];
}
int hh = 0, tt = 0;
q[0] = 0;
for (int i = 1; i <= n; ++i) {
int l = hh, r = tt;
while (l < r) {
int mid = l + r >> 1;
if (f[q[mid+1]] - f[q[mid]] > (t[i] + s) * (c[q[mid+1]] - c[q[mid]])) r = mid;
else l = mid + 1;
}
int j = q[r];
f[i] = f[j] - (t[i] + s) * c[j] + t[i] * c[i] + s * c[n];
while (hh < tt && (double)(f[q[tt]] - f[q[tt-1]]) * (c[i] - c[q[tt-1]]) >= (double)(f[i] - f[q[tt-1]]) * (c[q[tt]] - c[q[tt-1]])) tt--;
q[++tt] = i;
}
printf("%lld\n", f[n]);
return 0;
}
题目4:303运输小猫
C++代码如下,
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 100010, M = 100010, P = 110;
int n, m, p;
LL d[N], t[N], a[N], s[N];
LL f[P][M];
int q[M];
LL get_y(int k, int j) {
return f[j-1][k] + s[k];
}
int main() {
scanf("%d%d%d", &n, &m, &p);
for (int i = 2; i <= n; ++i) {
scanf("%lld", &d[i]);
d[i] += d[i-1];
}
for (int i = 1; i <= m; ++i) {
int h;
scanf("%d%lld", &h, &t[i]);
a[i] = t[i] - d[h];
}
sort(a + 1, a + m + 1);
for (int i = 1; i <= m; ++i) s[i] = s[i-1] + a[i];
memset(f, 0x3f, sizeof f);
for (int i = 0; i <= p; ++i) f[i][0] = 0;
for (int j = 1; j <= p; ++j) {
int hh = 0, tt = 0;
q[0] = 0;
for (int i = 1; i <= m; ++i) {
while (hh < tt && (get_y(q[hh+1], j) - get_y(q[hh], j)) <= a[i] * (q[hh+1] - q[hh])) hh++;
int k = q[hh];
f[j][i] = f[j-1][k] - a[i] * k + s[k] + a[i] * i - s[i];
while (hh < tt && (get_y(q[tt], j) - get_y(q[tt-1], j)) * (i - q[tt]) >=
(get_y(i, j) - get_y(q[tt], j)) * (q[tt] - q[tt-1])) tt--;
q[++tt] = i;
}
}
printf("%lld\n", f[p][m]);
return 0;
}