斜率优化dp

一、任务安排1

题目链接
在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 5010;
typedef long long LL;
int n,s;
LL t[N],c[N];
LL f[N];

int main()
{
    cin >> n >> s;
    for(int i = 1;i <= n;i ++)
    {
        cin >> t[i] >> c[i];
        t[i] += t[i - 1];
        c[i] += c[i - 1];
    }
    memset(f, 0x3f, sizeof f);
    f[0] = 0;
    for(int i = 1;i <= n;i ++)
    {
        for(int j = 0;j < i;j ++)
        f[i] = min(f[i], f[j] + t[i] * (c[i] - c[j]) + s * (c[n] - c[j]));
    }
    cout << f[n];
    
    return 0;
}

二、任务安排2

题目链接
在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 300010;
typedef long long LL;
LL t[N],c[N];
LL f[N];
int q[N];//队列
int n,s;

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;//默认队列已经进入了一个元素所以tt不用置为-1
    q[0] = 0;
    for(int i = 1;i <= n;i ++)//根据凸包函数,我们要保证f[i] - t[i] - c[i]最小//在斜率固定的情况下经过的点一定是一个外围的凸包,斜率为s + t[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;
}

三、任务安排3

题目链接
在这里插入图片描述

这里就加了一个二分优化,其他没什么变化

在这里插入图片描述

#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;
}

四、运输小猫

题目链接
在这里插入图片描述
在这里插入图片描述

#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; // n: 物品数量, m: 任务数量, p: 机器数量
LL d[N], t[N], a[N], s[N]; // d: 物品位置前缀和, t: 任务完成时间, a: 任务开始时间与物品位置差, s: a的前缀和
LL f[P][M]; // f[j][i] 表示前j个机器完成前i个任务的最小代价
int q[M]; // 斜率优化中的单调队列
 
// 计算决策点k在状态转移方程中的y值
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);
 
    // 计算a的前缀和,用于后续斜率计算
    for (int i = 1; i <= m; i ++ ) s[i] = s[i - 1] + a[i];
 
    // 初始化dp数组,没有机器时,完成任何任务都需要无穷大代价
    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; // 队列初始化为0,表示不使用任何任务
 
        for (int i = 1; i <= m; i ++ ) // 枚举任务数量
        {
            // 维护单调队列,找到最优决策点k
            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]; // 当前最优决策点
 
            // 更新dp状态
            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;
        }
    }
 
    // 输出结果,前p个机器完成前m个任务的最小代价
    printf("%lld\n", f[p][m]);
 
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

早睡早起^_^

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

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

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

打赏作者

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

抵扣说明:

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

余额充值