• 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);
    
    }


  • 相关阅读:
    linux如何给程序添加自启动
    nginx 反向代理apache服务器 配置java与PHP共存环境
    eclipse配置Js环境spket
    Linux下实现秒级定时任务的两种方案
    Linux时间戳和标准时间的互转
    thinkphp与php共享session
    安装PHP sphinx扩展 sphinx-1.1.0/sphinx.c:105:2: error: too few arguments 错误
    MySQLCouldn't find MySQL manager
    PHP 使用header函数设置HTTP头的示例方法 表头 (xlsx下载)
    JAVA正则表达式 Pattern和Matcher
  • 原文地址:https://www.cnblogs.com/chilumanxi/p/5136064.html
Copyright © 2020-2023  润新知