Description
Solution
设
si=∑ij=1pj
,
di=di−1+si−1
那么得到状态转移方程:
dpi=min{dpj+cj+di−dj−sj×(xi−xj)}
斜率优化即可。
Code
/**************************
Au: Hany01
Date: Jan 7th, 2018
Prob: bzoj1096
Email: hany01@foxmail.com
**************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Ha (1000000007)
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
inline void File()
{
#ifdef hany01
freopen("bzoj1096.in", "r", stdin);
freopen("bzoj1096.out", "w", stdout);
#endif
}
const int maxn = 1000005;
LL X[maxn], s[maxn], c[maxn], d[maxn], dp[maxn];
int q[maxn];
#define y(j) (dp[j] - d[j] + s[j] * X[j])
#define x(j) (s[j])
inline double slope(int j, int k) { return (y(k) - y(j)) * 1.0 / (x(k) - x(j)); }
int main()
{
File();
register int n, j, head, tail;
n = read();
For(i, 1, n)
X[i] = read(), s[i] = s[i - 1] + read()/*p[i]*/,
c[i] = read(), d[i] = d[i - 1] + s[i - 1] * (X[i] - X[i - 1]);
dp[0] = 0; q[head = tail = 0] = 0;
For(i, 1, n) {
while (head < tail && slope(q[head], q[head + 1]) <= X[i]) ++ head;
j = q[head];
dp[i] = dp[j] + c[i] + d[i] - d[j] - s[j] * (X[i] - X[j]);
q[++ tail] = i;
while (head + 1 < tail && slope(q[tail - 2], q[tail - 1]) >= slope(q[tail - 1], q[tail]))
q[tail - 1] = q[tail], -- tail;
}
printf("%lld\n", dp[n]);
return 0;
}
//叹流年、又成虚度。
// -- 陆游《谢池春·壮岁从戎》