POJ 3616 Milking Time

本文介绍了一种通过动态规划算法来解决奶牛在限定时间内最大化产奶量的问题。奶牛需要在N小时内安排挤奶活动,期间要考虑休息时间及不同时间段的挤奶效率。

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

Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤ 1,000,000) hours (conveniently labeled 0..N-1) so that she produces as much milk as possible.

Farmer John has a list of M (1 ≤ M ≤ 1,000) possibly overlapping intervals in which he is available for milking. Each interval i has a starting hour (0 ≤ starting_houri ≤ N), an ending hour (starting_houri < ending_houri ≤ N), and a corresponding efficiency (1 ≤ efficiencyi ≤ 1,000,000) which indicates how many gallons of milk that he can get out of Bessie in that interval. Farmer John starts and stops milking at the beginning of the starting hour and ending hour, respectively. When being milked, Bessie must be milked through an entire interval.

Even Bessie has her limitations, though. After being milked during any interval, she must rest R (1 ≤ R ≤ N) hours before she can start milking again. Given Farmer Johns list of intervals, determine the maximum amount of milk that Bessie can produce in the N hours.

Input


* Line 1: Three space-separated integers: NM, and R
* Lines 2..M+1: Line i+1 describes FJ's ith milking interval withthree space-separated integers: starting_houri , ending_houri , and efficiencyi

Output


* Line 1: The maximum number of gallons of milk that Bessie can product in the Nhours

Sample Input


12 4 2
1 2 8
10 12 19
3 6 24
7 10 31

Sample Output


43

一只奶牛要规划自己的产奶时间。它要在n小时内产出最多的奶。给出m段时间内能产的奶,每次产奶后要休息r个小时。

首先是数据的处理。每次产奶后都要休息r个小时,所以不妨直接给结束时间加上r个小时。

然后考虑如何dp。我一开始考虑的是针对时间dp,开数组统计每个时间最优解。但这样显然不现实,由于时间是连续的会很复杂。后来就想干脆对给的几段进行dp。因为每次只能进行一段,所以每段时间内的最优解都是相同的。每段时间最优解初始值就是这段时间的产奶量。然后有结束时间<=开始时间的时间段(说明在本次产奶之前) 就做一次dp,就能求出每段时间的最优解了。当然,为了实现这点需要对时间段进行排序,把最先开始的排到前面去。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct milk
{
    int start,endd,milk;
}a[1010];
int dp[1010];
int cmp(milk a,milk b)
{
    if(a.start==b.start) return a.endd<b.endd;
    return a.start<b.start;
}
int main()
{
    int m,n,r;
    int maxx=0;
    while(scanf("%d %d %d",&m,&n,&r)!=EOF&&m)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d %d %d",&a[i].start,&a[i].endd,&a[i].milk);
            a[i].endd+=r;
        }
        sort(a,a+n,cmp);
        for(int i=0;i<n;i++)
        {
            dp[i]=a[i].milk;
            for(int j=0;j<i;j++)
            {
                if(a[j].endd<=a[i].start)
                    dp[i]=max(dp[i],dp[j]+a[i].milk);
            }
        }
        for(int i=0;i<n;i++)
            maxx=max(maxx,dp[i]);
        cout<<maxx<<endl;
    }
    return 0;
}

一开始写的时候忘记加eof,结果Output Limit Exceeded了233,没见过的船新错误

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值