BZOJ 1096 [ZJOI2007]仓库建设 斜率优化dp

2/22/2017来源:ASP.NET技巧人气:3918

/* bzoj 1096 http://www.lydsy.com/JudgeOnline/PRoblem.php?id=1096 Si为P的前缀和 Bi为X*P的前缀和 Fi=min{Fj+(Si-Sj*Xi-(Bi-Bj)+Ci)} Fj+Bj=Fi-Si*Xi+Bi-Ci+Sj*Xi y=Fj+Bj x=Sj k=Xi b=Fi-Si*Xi+Bi-Ci */ #include <cstdio> #include <cstring> #include <algorithm> #include <cstring> #include <cmath> #define MAXN 1000005 #define N 100 #define LL long long #define INF 1000000005 #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) using namespace std; const double eps = 1e-8; LL read() { LL t=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){t=t*10+ch-'0';ch=getchar();} return t*f; } LL n,l=1,r=0; LL f[MAXN],q[MAXN],b[MAXN],c[MAXN],p[MAXN],x[MAXN],s[MAXN]; double getk(int i,int j) { return (double)(f[i]+b[i]-f[j]-b[j])/(double)(s[i]-s[j]); } int main() { n=read(); for(int i=1;i<=n;i++) x[i]=read(),p[i]=read(),c[i]=read(); for(int i=1;i<=n;i++) s[i]=s[i-1]+p[i],b[i]=b[i-1]+x[i]*p[i]; q[++r]=0; for(int i=1;i<=n;i++) { while(l<r&&getk(q[l],q[l+1])<x[i]) l++; int j=q[l]; f[i]=f[j]+(s[i]-s[j])*x[i]-(b[i]-b[j])+c[i]; while(l<r&&getk(i,q[r])<getk(q[r],q[r-1])) r--; q[++r]=i; } printf("%lld",f[n]); return 0; }