• POJ 3616 Milking Time


    Milking Time

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6319   Accepted: 2660

    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_houriN), an ending hour (starting_houri < ending_houriN), 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 ≤ RN) 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: 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

    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

    这个题目意思是给定一些工作的开始时间,结束时间,和效率,当每次进行完一次工作以后需要休息,问在规定的时间内能进行的最大的效率的值。

    DP轻松解决,排序一下开始时间,然后用一个一维的dp数组表示在第i个工作为最后一个工作的最大效率值

    在符合上一个工作的结束时间加上休息时间在当前的工作的开始时间的前提下,有转移方程:

    dp[i] = max(dp[i], dp[j] + data[i].ef)   其中j是i前面的工作

    代码如下:

    /*************************************************************************
    	> File Name: Milking_Time.cpp
    	> Author: Zhanghaoran
    	> Mail: chilumanxi@xiyoulinux.org
    	> Created Time: Wed 28 Oct 2015 10:00:38 PM CST
     ************************************************************************/
    
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    
    using namespace std;
    
    struct node{
        unsigned long long st, et, ef;
    }data[1010];
    
    int N, M, R;
    unsigned long long dp[1010];
    
    int max(unsigned long long a, unsigned long long b){
        return a > b ? a : b;
    }
    bool cmp(node a, node b){
        if(a.st < b.st)
            return true;
        else
            return false;
    }
    
    int main(void){
        scanf("%d%d%d", &N, &M, &R);
        int Max = 0;
        for(int i = 1; i <= M; i ++){
            scanf("%llu%llu%llu", &data[i].st, &data[i].et, &data[i].ef);
        }
        sort(data + 1, data + M + 1, cmp);
        for(int i = 1; i <= M; i ++){
            dp[i] = data[i].ef;
        }
        for(int i = 2; i <= M; i ++){
            for(int j = 1; j < i; j ++){
                if(data[j].et + R <= data[i].st){
                    dp[i] = max(dp[i], dp[j] + data[i].ef);
                }
            }
            if(Max < dp[i])
                Max = dp[i];
        }
    
        printf("%d
    ", Max);
    
    }


  • 相关阅读:
    “嫦娥一号”探月卫星成功发射
    优化SQL Server数据库查询(转)
    虚拟网络连接设置
    字符串分割自定义函数(SQL)
    做程序的劫客
    Linux学习笔记12我的第一个C程序
    C#学习笔记——25个经典问题
    C#学习笔记——回调机制
    C#学习笔记——TCP通讯
    halcon学习笔记——实例篇(2)长度和角度测量
  • 原文地址:https://www.cnblogs.com/chilumanxi/p/5136064.html
Copyright © 2020-2023  润新知