【BZOJ 1096】【ZJOI 2007】仓库建设 DP+斜率优化

时间:2023-03-09 03:40:28
【BZOJ 1096】【ZJOI 2007】仓库建设 DP+斜率优化

后缀自动机看不懂啊QAQ

放弃了还是看点更有用的东西吧,比如斜率优化DP

先水一道

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1000003
#define read(x) x=getint()
using namespace std;
typedef long long LL;
inline LL getint() {
LL k = 0; int fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = k * 10 + c - '0';
return k * fh;
}
LL x[N], p[N], c[N], sum[N], b[N], f[N], n, q[N];
inline LL fz(int x, int y) {
return f[x] + b[x] - f[y] - b[y];
}
inline LL fm(int x, int y) {
return sum[x] - sum[y];
}
int main() {
read(n);
for(int i = 1; i <= n; ++i)
read(x[i]), read(p[i]), read(c[i]);
for(int i = 1; i <= n; ++i)
sum[i] = sum[i - 1] + p[i], b[i] = b[i - 1] + p[i] * x[i];
int h = 0, t = 1, now;
for(int i = 1; i <= n; ++i) {
while (h < t - 1 && fz(q[h + 1], q[h]) < x[i] * fm(q[h + 1], q[h]))
++h;
now = q[h];
f[i] = f[now] + (sum[i] - sum[now]) * x[i] - b[i] + b[now] + c[i];
while (h < t - 1 && fz(q[t - 1], q[t - 2]) * fm(i, q[t - 1]) > fz(i, q[t - 1]) * fm(q[t - 1], q[t - 2]))
--t;
q[t++] = i;
}
printf("%lld\n", f[n]);
return 0;
}

我好蒟蒻啊,省选要爆零!