大一暑假第二周动态规划 Bessie is such a hard-working cow. In fact, she is so focused on maximizing her producti

C - Milking Time

动态规划+贪心思想。动态规划,YYDS
题目:
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.

输入:

  • Line 1: Three space-separated integers: N, M, 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

输出:

  • Line 1: The maximum number of gallons of milk that Bessie can product in the N hours

样例:
输入:
12 4 2
1 2 8
10 12 19
3 6 24
7 10 31
输出:
43

————————————————————————————————————————
题意:
农夫有一头母牛,可以用来挤奶;现在农夫将在接下来的时间n中进行挤奶,农夫手里有m个挤奶方案,每个方案记录了开始时间和结束时间还有挤奶量;当然奶牛也不能一刻不停的产奶,它在每一次挤奶过后都需要休息r小时;根据这些已知条件,请求出最大的挤奶量。

思路:
这牛不得累死了
看到“最大”这些词眼,肯定就是贪心思想了。
类似于01背包,每一个方案只能用一次。先把方案按时间顺序升序排序(排开始时间或者结束时间都行),接下来就是dp的模板了,注意的是:dp到第i个计划时,最起码可以执行它本身。判断条件有:f[j].b+r<=f[i].a;
接下来:dp[i]=max(dp[i],dp[j]+f[i].s);
最后输出其中的最大值。
挤完奶牛也累死了
————————————————————————————————————————
代码:

//类似01背包
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<string.h>
#include<string>
#include<map>
#include<algorithm>
#define inf 99999999
using namespace std;
int dp[1010];//i代表第i个列表方案
struct ppp
{
    int a,b,s;
}f[1010];
bool cmp(ppp x,ppp y)//结束时间升序
{
    if(x.b==y.b)
        return x.a<y.a;
    return x.b<y.b;
}
int main()
{
    int n,m,r,i,j;
    int k,t,maxx=0;
    scanf("%d%d%d",&n,&m,&r);
    memset(dp,0,sizeof(dp));
    for(i=0;i<m;i++)
    {
        scanf("%d%d%d",&f[i].a,&f[i].b,&f[i].s);
    }
    sort(f,f+m,cmp);
    for(i=0;i<m;i++)
    {
        dp[i]=f[i].s;//第i个计划最起码可以执行它本身
        for(j=0;j<i;j++)
        {
            if(f[j].b+r<=f[i].a)//i之前的计划结束时间+r与i的起始时间比较
            {
                dp[i]=max(dp[i],dp[j]+f[i].s);
                //
            }
        }
        maxx=max(maxx,dp[i]);
    }
    printf("%d\n",maxx);
    return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值