# bzoj 1096: [ZJOI2007]仓库建设

dp是很好想的了，关键是数据太大，普通dp肯定超时，所以一定有用某种优化，dp优化也就那么几种，这道题用的是斜率优化，先写出普通的状态转移方程： dp[i] = min{  dp[j] + Σ ( p[k] * (x[i] - x[k] ) ) ,  j+1 <=k <= i ,  0 <= j <= i-1}

Σ ( p[k] * (x[i] - x[k] ) = Σ (p[k] * x[i] - p[k] * x[k]) = p[ j+1 ~ i] * x[i] - p[ j+1~i] * x[ j+1 ~i]

dp[i] = min { dp[j] + x[i] * (a[i] - a[j]) - (b[i] - b[j]) }

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#define N 1000100
using namespace std;

int n;
deque<int> q;
deque<int>::iterator x, y;
long long a[N]={0}, b[N]={0};
long long f[N], dis[N], c[N];

long long getup(int j, int k) { return f[j] - f[k] + b[j] - b[k]; }
long long getdown(int j, int k) { return a[j] - a[k]; }
long long getans(int j, int now) { return f[j] + (a[now] - a[j]) * dis[now] -b[now] + b[j] +c[now]; }
bool ketan(int j, int k, int now) { return getup(j, k) < dis[now] * getdown(j, k); }
bool keya(int i, int j, int k) { return getup(i, j) * getdown (j, k) > getup(j, k) * getdown(i, j); }

int main()
{
scanf("%d",&n);
for (int i = 1; i <= n; ++i)
{
long long z;
scanf("%lld%lld%lld", &dis[i], &z, &c[i]);
a[i] = a[i-1] + z; b[i] = b[i-1] + z*dis[i];
}
f[0] = 0; q.push_front(0);
for (int i = 1; i <= n; ++i)
{
while (q.size() > 1)
{
x = q.begin(); y = x; y++;
if (!ketan(*y, *x, i)) break;
q.pop_front();
}
x = q.begin();
f[i] = getans(*x, i);
while (q.size() > 1)
{
x = q.end(); x--; y = x; y--;
if (!keya(*y, *x, i)) break;
q.pop_back();
}
q.push_back(i);
}
printf("%lld\n", f[n]);
}

posted @ 2014-05-09 09:03  handsomeJian  阅读(140)  评论(0编辑  收藏