zoukankan      html  css  js  c++  java
  • Milking Time

    Description

    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 N hours

    Sample Input

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

    Sample Output

    43
    /*
    题意:奶牛在0~n的区间内产奶,农场主有m个时间段可以挤奶并且给出第i个时间段的挤奶产量,农场主每次挤完奶之后必须休息时间r之后
        才能工作,问农场主最多可以挤多少奶
    
    初步思路:贪心
    
    #错误:贪心解决不了问题还是得动态规划,dp[i]表示第i个时间段能获得的最大奶量,dp[i]=max(dp[i],dp[j]+fr[i].val);
        注意这个dp[j]的结束时间不能和dp[i]的开始时间冲突
    */
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    struct node{
        int l,r,val;
        bool operator < (const node & other) const{
            if(l==other.l) return r<other.r;
            return l<other.l;
        }   
    }fr[1005];
    int dp[1005];
    int m,n,r;
    int maxn;
    void init(){
        memset(dp,0,sizeof dp);
        maxn=-1;
    }
    int main(){
        // freopen("in.txt","r",stdin);
        while(scanf("%d%d%d",&n,&m,&r)!=EOF){
            init();
            for(int i=1;i<=m;i++){
                scanf("%d%d%d",&fr[i].l,&fr[i].r,&fr[i].val);
                fr[i].r+=r;
            }
            sort(fr+1,fr+m+1);
            for(int i=1;i<=m;i++){
                dp[i]=fr[i].val;
                for(int j=1;j<i;j++){
                    if(fr[j].r<=fr[i].l){
                        dp[i]=max(dp[i],dp[j]+fr[i].val);
                    }
                }
                maxn=max(dp[i],maxn);
            }
            printf("%d
    ",maxn);
        }
        return 0;
    }
  • 相关阅读:
    idea 快捷键 记录
    Spring Boot 之注解@Component @ConfigurationProperties(prefix = "sms") 使用@ConfigurationProperties读取yml配置
    SpringData Jdbc
    设备树中指定的中断触发方式与request_irq中指定的触发方式不一致时,内核会使用哪种中断触发方式呢?
    设备树中的interrupts属性解析
    编译grub时报告"grub_script.yy.c:19:22: error: statement with no effect [-Werror=unused-value]"怎么处理?
    uefi是如何启动linux内核的?
    markdown中如何设置字体为红色?
    linux下如何查看磁盘分区所使用的文件系统格式?
    bootargs中的rootwait 与rootdelay有什么区别?
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6581972.html
Copyright © 2011-2022 走看看