【ZJOI2007】【BZOJ1096】仓库建设

【题目链接】

【前置技能】

  • 斜率优化DP

【题解】

  • 设计状态 f [ i ] f[i] f[i]表示在第 i i i个工厂建立仓库,且 1 1 1 ~ i i i号工厂中的产品都已存储好的最少花费。答案就是 f [ n ] f[n] f[n]
  • 转移方程 f [ i ] = m i n { f [ j ] + c o s t ( j , i ) } + c [ i ] f[i] = min\{f[j] + cost(j, i)\}+c[i] f[i]=min{f[j]+cost(j,i)}+c[i],其中 c o s t ( j , i ) cost(j, i) cost(j,i)表示将 j + 1 j + 1 j+1 ~ i i i号工厂的产品运输到 i i i号工厂的花费。令 s u m [ i ] sum[i] sum[i]表示将 1 1 1 ~ i i i号工厂中的产品从 1 1 1号工厂运送到各自工厂的花费, p p p数组做一遍前缀和,那么 c o s t ( j , i ) = ( p [ i ] − p [ j ] ) ∗ x [ i ] − ( s u m [ i ] − s u m [ j ] ) cost(j, i) = (p[i] - p[j]) * x[i] - (sum[i] - sum[j]) cost(j,i)=(p[i]p[j])x[i](sum[i]sum[j])。转移方程整理后可得 f [ i ] = m i n { f [ j ] + s u m [ j ] − p [ j ] ∗ x [ i ] } + c [ i ] − s u m [ i ] + p [ i ] ∗ x [ i ] f[i] = min\{f[j]+sum[j]-p[j]*x[i]\}+c[i]-sum[i]+p[i]*x[i] f[i]=min{f[j]+sum[j]p[j]x[i]}+c[i]sum[i]+p[i]x[i]
  • ( p [ i ] , f [ i ] + s u m [ i ] ) (p[i],f[i]+sum[i]) (p[i],f[i]+sum[i])为坐标,维护下凸壳,用 x [ i ] x[i] x[i]切凸壳进行转移即可。
  • 时间复杂度 O ( N ) O(N) O(N)

【代码】

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL  long long
#define MAXN    1000010
using namespace std;
struct dot{LL x, y; int id;}q[MAXN];
int n, x[MAXN], c[MAXN], l, r;
LL sum[MAXN], f[MAXN], p[MAXN];
 
dot operator - (dot a, dot b){
    a.x -= b.x, a.y -= b.y;
    return a;
}
 
LL operator * (dot a, dot b){
    return a.x * b.y - a.y * b.x;
}
 
template <typename T> void chkmin(T &x, T y){x = min(x, y);}
template <typename T> void chkmax(T &x, T y){x = max(x, y);}
template <typename T> void read(T &x){
    x = 0; int f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}
 
int main(){
    read(n);
    for (int i = 1; i <= n; ++i)
        read(x[i]), read(p[i]), read(c[i]);
    for (int i = 1; i <= n; ++i)
        sum[i] = 1ll * p[i] * x[i];
    for (int i = 1; i <= n; ++i)
        sum[i] += sum[i - 1];
    for (int i = 1; i <= n; ++i)
        p[i] += p[i - 1];
    f[0] = 0;
    q[l = r = 0] = dot{0, 0, 0};
    for (int i = 1; i <= n; ++i){
        while (l < r && 1ll * x[i] * (q[l + 1].x - q[l].x) >= q[l + 1].y - q[l].y) ++l;
        int cur = q[l].id;
        f[i] = f[cur] + sum[cur] - p[cur] * x[i] + c[i] - sum[i] + p[i] * x[i];
        dot tmp = (dot){p[i], f[i] + sum[i], i};
        while (l < r && (tmp - q[r]) * (q[r] - q[r - 1]) >= 0) --r;
        q[++r] = tmp;
    }
    printf("%lld\n", f[n]);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值