# BZOJ 1096 ZJOI2007 仓库建设 边坡优化

f[j] + sumxp[j] = x[i]*sump[j] + sumxp[i] - x[i]*sump[i] - C[i] + f[i]

X[j]=sump[j]
Y[j]=f[j]+sumxp[j]
s[i]=x[i]

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define M 1001001
using namespace std;
typedef long long ll;
typedef pair<ll,ll> abcd;
int n,X[M],P[M],C[M];
ll sumP[M],sumXP[M],f[M];
abcd q[M];int r,h;
double Get_Slope(const abcd &x,const abcd &y)
{
return (double)(x.second-y.second)/(x.first-y.first);
}
void Insert(abcd x)
{
while(r-h>1)
{
if( Get_Slope(q[r],x)<Get_Slope(q[r-1],q[r]) )
q[r--]=q[0];
else
break;
}
q[++r]=x;
}
abcd Get_Ans(double s)
{
while(r-h>1)
{
if(Get_Slope(q[h+1],q[h+2])<s)
q[++h]=q[0];
else
break;
}
return q[h+1];
}
int main()
{
int i;
cin>>n;
for(i=1;i<=n;i++)
scanf("%d%d%d",&X[i],&P[i],&C[i]);
for(i=1;i<=n;i++)
{
sumP[i]=sumP[i-1]+P[i];
sumXP[i]=sumXP[i-1]+(ll)X[i]*P[i];
}
for(i=1;i<=n;i++)
{
Insert( make_pair(sumP[i-1],f[i-1]+sumXP[i-1]) );
abcd p=Get_Ans(X[i]);
f[i] = p.second + X[i]*sumP[i] - X[i]*p.first - sumXP[i] + C[i] ;
}
cout<<f[n]<<endl;
}


posted @ 2015-09-23 19:09  lcchuguo  阅读(102)  评论(0编辑  收藏