POJ-3616 Milking Time ( DP )

题目链接: http://poj.org/problem?id=3616

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

题目大意 有一头奶牛,它有m个工作时间段,每个工作时间可以产一定量的奶,但是它每次工作后都需要休息时间r,求奶牛产奶的最大值
水题 对奶牛工作时间段按开始时间升序排序后,简单DP即可,直接放代码,状态转移方程就不写了

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

class Node{
public:
    int begin, end, eff;
}work[1005];

bool cmp( Node a, Node b ){
    return a.begin < b.begin;
}

int dp[1005];

int main(){
    ios::sync_with_stdio( false );

    int n, m, r;

    while( cin >> n >> m >> r ){
        for( int i = 1; i <= m; i++ )
            cin >> work[i].begin >> work[i].end >> work[i].eff;
        sort( work + 1, work + m + 1, cmp );
        work[0].begin = work[0].end = work[0].eff;
        
        dp[0] = 0;
        int maxi;
        for( int i = 1; i <= m; i++ ){
            maxi = 0;
            for( int j = 0; j < i; j++ ){
                if( work[i].begin >= work[j].end + r && dp[j] > dp[maxi] )
                    maxi = j;
            }
            dp[i] = dp[maxi] + work[i].eff;
        }

        int ans = 0;
        for( int i = 1; i <= m; i++ )
            ans = max( ans, dp[i] );

        cout << ans << endl;
    }

    return 0;
}

原文地址:https://www.cnblogs.com/hollowstory/p/5450456.html