# ●BZOJ 1096 [ZJOI2007]仓库建设

http://www.lydsy.com/JudgeOnline/problem.php?id=1096

$（d_i：i 位置到1位置的距离，p_i：i位置的成品数量，c_i：i位置建仓库的费用）$

$DP[i]=min(DP[j]+W(j+1{～}i位置的物品挪动到i位置的代价)+c_i)$

$W=(d_i-d_{i})p_{i}+(d_i-d_{i-1})p_{i-1}+\cdots+(d_i-d_{j+1})p_{j+1}$

$\quad=d_i(p_i+p_{i-1}+\cdots+p_{j+1})-(d_{i}p_{i}+d_{i-1}p_{i-1}+\cdots+d_{j+1}p_{j+1})$

$W=d_i(sump_i-sump_j)-(sumdp_i-sumdp_j)$

$\quad=d_isump_i-sumdp_i-d_isump_j+sumdp_j$

$DP[i]=(d_isump_i-sumdp_i+c_i)-(d_isump_j)+(sumdp_j+DP[j]))$

$Y_j-d_isump_j-(Y_k-d_isump_k)<0$

#include<cstdio>
#include<cstring>
#include<iostream>
#define MAXN 1000050
#define ll long long
using namespace std;
ll d[MAXN],p[MAXN],c[MAXN];
ll DP[MAXN],sump[MAXN],sumdp[MAXN];
int N;
struct Moque{
int q[MAXN],l,r;
#define Y(j) (DP[j]+sumdp[j])
#define X(j) (sump[j])
#define Slope(j,k) (1.0*Y(j)-Y(k))/(1.0*X(j)-X(k))
void Reset(){l=r=1;q[1]=0;}
void Push(int i){
if(l<=r&&X(i)==X(q[r]))
{if(Y(i)<Y(q[r])) r--; else return;}
while(l+1<=r&&Slope(i,q[r])<Slope(q[r],q[r-1])) r--;
q[++r]=i;
}
int Query(int i){
while(l+1<=r&&Slope(q[l+1],q[l])<d[i]) l++;
return q[l];
}
}Q;
static int sn; static char ch;
x=0; sn=1; ch=getchar();
while(ch<'0'||'9'<ch){if(ch=='-')sn=-1;ch=getchar();}
while('0'<=ch&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
x=x*sn;
}
int main(){
scanf("%d",&N);
for(int i=1;i<=N;i++){
sump[i]=p[i]+sump[i-1];
sumdp[i]=d[i]*p[i]+sumdp[i-1];
}
Q.Reset();
for(int i=1,j;i<=N;i++){
j=Q.Query(i);
DP[i]=d[i]*sump[i]-sumdp[i]+c[i]-d[i]*sump[j]+sumdp[j]+DP[j];
Q.Push(i);
}
printf("%lld",DP[N]);
return 0;
}


posted @ 2017-12-27 20:15  *ZJ  阅读(45)  评论(0编辑  收藏