acwing算法提高之动态规划--斜率优化DP

123 篇文章 1 订阅
文章介绍了如何使用C++实现斜率优化的动态规划方法解决三个问题:任务安排1和2,以及运输小猫问题。通过示例展示了状态定义、初始化、转移和求解的过程。
摘要由CSDN通过智能技术生成

1 专题说明

本专题用来记录使用斜率优化的DP问题。

2 训练

题目1300任务安排1

C++代码如下,

#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n, s;
    cin >> n >> s;
    
    vector<int> sumc(n + 10, 0), sumt(n + 10, 0);
    vector<long long> f(n + 10, 0);
    for (int i = 1; i <= n; ++i) {
        int t, c;
        cin >> t >> c;
        sumt[i] = sumt[i-1] + t;
        sumc[i] = sumc[i-1] + c;
    }
    
    //状态定义f[i]:将前i个任务处理完的所有方案的集合,属性为代价最小。
    
    //状态初始化
    for (int i = 0; i < f.size(); ++i) {
        f[i] = 0x3f3f3f3f3f3f3f3f; //long long是8个字节
    }
    f[0] = 0;
    
    //状态转移
    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < i; ++j) {
            f[i] = min(f[i], f[j] + (long long)sumt[i] * (sumc[i] - sumc[j]) + (long long)s * (sumc[n] - sumc[j]));
        }
    }
    
    //最终答案
    cout << f[n] << endl;
    
    return 0;
}

题目2301任务安排2

C++代码如下,

#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 300010;

int n, s;
LL c[N], t[N];
LL f[N];
int q[N];

int main() {
    scanf("%d%d", &n, &s);
    for (int i = 1; i <= n; ++i) {
        scanf("%lld%lld", &t[i], &c[i]);
        t[i] += t[i-1];
        c[i] += c[i-1];
    }
    
    int hh = 0, tt = 0;
    q[0] = 0;
    
    for (int i = 1; i <= n; ++i) {
        while (hh < tt && (f[q[hh+1]] - f[q[hh]]) <= (t[i] + s) * (c[q[hh+1]] - c[q[hh]])) hh++;
        int j = q[hh];
        f[i] = f[j] - (t[i] + s) * c[j] + t[i] * c[i] + s * c[n];
        while (hh < tt && (__int128)(f[q[tt]] - f[q[tt-1]]) * (c[i] - c[q[tt-1]]) >= (__int128)(f[i] - f[q[tt-1]]) * (c[q[tt]] - c[q[tt-1]])) tt--;
        q[++tt] = i;
    }
    
    printf("%lld\n", f[n]);
    
    return 0;
}

题目3302任务安排3

C++代码如下,

#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 300010;

int n, s;
LL t[N], c[N];
LL f[N];
int q[N];

int main() {
    scanf("%d%d", &n, &s);
    for (int i = 1; i <= n; ++i) {
        scanf("%lld%lld", &t[i], &c[i]);
        t[i] += t[i-1];
        c[i] += c[i-1];
    }
    
    int hh = 0, tt = 0;
    q[0] = 0;
    
    for (int i = 1; i <= n; ++i) {
        int l = hh, r = tt;
        while (l < r) {
            int mid = l + r >> 1;
            if (f[q[mid+1]] - f[q[mid]] > (t[i] + s) * (c[q[mid+1]] - c[q[mid]])) r = mid;
            else l = mid + 1;
        }
        
        int j = q[r];
        f[i] = f[j] - (t[i] + s) * c[j] + t[i] * c[i] + s * c[n];
        while (hh < tt && (double)(f[q[tt]] - f[q[tt-1]]) * (c[i] - c[q[tt-1]]) >= (double)(f[i] - f[q[tt-1]]) * (c[q[tt]] - c[q[tt-1]])) tt--;
        q[++tt] = i;
    }
    
    printf("%lld\n", f[n]);
    
    return 0;
}

题目4303运输小猫

C++代码如下,

#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 100010, M = 100010, P = 110;

int n, m, p;
LL d[N], t[N], a[N], s[N];
LL f[P][M];
int q[M];

LL get_y(int k, int j) {
    return f[j-1][k] + s[k];
}

int main() {
    scanf("%d%d%d", &n, &m, &p);
    
    for (int i = 2; i <= n; ++i) {
        scanf("%lld", &d[i]);
        d[i] += d[i-1];
    }
    
    for (int i = 1; i <= m; ++i) {
        int h;
        scanf("%d%lld", &h, &t[i]);
        a[i] = t[i] - d[h];
    }
    
    sort(a + 1, a + m + 1);
    
    for (int i = 1; i <= m; ++i) s[i] = s[i-1] + a[i];
    
    memset(f, 0x3f, sizeof f);
    for (int i = 0; i <= p; ++i) f[i][0] = 0;
    
    for (int j = 1; j <= p; ++j) {
        int hh = 0, tt = 0;
        q[0] = 0;
        
        for (int i = 1; i <= m; ++i) {
            while (hh < tt && (get_y(q[hh+1], j) - get_y(q[hh], j)) <= a[i] * (q[hh+1] - q[hh])) hh++;
            int k = q[hh];
            f[j][i] = f[j-1][k] - a[i] * k + s[k] + a[i] * i - s[i];
            while (hh < tt && (get_y(q[tt], j) - get_y(q[tt-1], j)) * (i - q[tt]) >= 
                (get_y(i, j) - get_y(q[tt], j)) * (q[tt] - q[tt-1])) tt--;
            q[++tt] = i;
        }
    }
    
    printf("%lld\n", f[p][m]);
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YMWM_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值