Poj 3616 (dp)

Milking Time
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3603   Accepted: 1530

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

Source

USACO 2007 November Silver

入门dp。因为每头牛做完一件事后要休息一段时间,所以单纯的以dp[i]来表示到时间i的最大受益是有后效性的,那么再加一维状态表示选到i后能否再选就可以了。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
const int maxn = 1000000 + 5;
const int INF = 2000000000;
const int Mod = 1000000000;
typedef pair<int,int> P;
typedef long long LL;

int dp[maxn][2];
vector<P> v[maxn];
vector<P> ve[maxn];
int main(){
    int n,m,r;
    while(scanf("%d%d%d",&n,&m,&r) != EOF){
        for(int i = 0;i < maxn;i++){
            v[i].clear();
            ve[i].clear();
        }
        for(int i = 0;i < m;i++){
            int s,e,val;
            scanf("%d%d%d",&s,&e,&val);
            if(e+r <= n) v[e+r].push_back(P(val,s));
            ve[e].push_back(P(val,s));
        }
        dp[0][0] = 0;
        dp[0][1] = 0;
        int ans = 0;
        for(int i = 1;i <= n;i++){
            int Max = -1;
            for(int j = 0;j < v[i].size();j++){
                P p = v[i][j];
                int from = p.second;
                int val = p.first;
                Max = max(Max,dp[from][1]+val);
            }
            dp[i][1] = max(dp[i-1][1],Max);
            ans = max(ans,dp[i][1]);
            Max = -1;
            for(int j = 0;j < ve[i].size();j++){
                P p = ve[i][j];
                int from = p.second;
                int val = p.first;
                Max = max(Max,dp[from][1]+val);
            }
            dp[i][0] = max(max(dp[i-1][0],dp[i-1][1]), Max);
            ans = max(ans,dp[i][0]);
        }
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(Poj 3616 (dp))