hdu 3616 Milking Time dp 离散化

本文介绍了一个通过离散化与动态规划解决的产奶问题。问题描述为在给定的时间段内,牛在不同的时间段内产奶,每次产奶后需要休息一段时间才能再次产奶。目标是在限定时间内获取最多的牛奶。文章详细解释了如何通过排序和动态规划算法求解此问题,并给出了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

题目链接:http://poj.org/problem?id=3616

题目来源:《挑战》练习题

简要题意:牛可在 M [si,ei)区间分别产 vi 牛奶,产后休息 R 时间可继续产。
    产奶不相交,问 N 时间内最多获得多少奶。

数据范围:1M103;1N106;1RN;0si<eiN;vi106

题解

显然 N 没啥用,我们可以直接离散化。

然后按照开始时间排个序从前往后扫一遍就行了。

实现

有实现是扫前面的区间去dp的,这个复杂度比较高,是平方的。

题目表述开始没完全明白,对ei多加了个 1 结果WA了一发。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
struct Node {
    int l, r, v;
    bool operator<(const Node &o) const {
        return l < o.l;
    }
};

Node a[1005];
int dis[2005];
int dp[2005];

int main()
{
    int n, m, r;
    scanf("%d%d%d", &n, &m, &r);
    for (int i = 0; i < m; i++) {
        scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].v);
        a[i].r += r;
        dis[i] = a[i].l, dis[i+m] = a[i].r;
    }
    sort(a, a+m);
    sort(dis, dis+2*m);
    n = unique(dis, dis+2*m)-dis;
    for (int i = 0; i < m; i++) {
        a[i].l = lower_bound(dis, dis+n, a[i].l)-dis+1;
        a[i].r = lower_bound(dis, dis+n, a[i].r)-dis+1;
    }

    int x = 0;
    for (int i = 1; i <= n; i++) {
        dp[i] = max(dp[i], dp[i-1]);
        while (x < m && a[x].l == i) {
            dp[a[x++].r] = max(dp[a[x].r], dp[i]+a[x].v);
        }
    }
    printf("%d\n", dp[n]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值