[BZOJ1642][Usaco2007 Nov]Milking Time 挤奶时间

1642: [Usaco2007 Nov]Milking Time 挤奶时间

Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 878  Solved: 507 [Submit][Status][Discuss]

Description

贝茜是一只非常努力工作的奶牛,她总是专注于提高自己的产量。为了产更多的奶,她预计好了接下来的N (1 ≤ N ≤ 1,000,000)个小时,标记为0..N-1。 Farmer John 计划好了 M (1 ≤ M ≤ 1,000) 个可以挤奶的时间段。每个时间段有一个开始时间(0 ≤ 开始时间 ≤ N), 和一个结束时间 (开始时间 < 结束时间 ≤ N), 和一个产量 (1 ≤ 产量 ≤ 1,000,000) 表示可以从贝茜挤奶的数量。Farmer John 从分别从开始时间挤奶,到结束时间为止。每次挤奶必须使用整个时间段。 但即使是贝茜也有她的产量限制。每次挤奶以后,她必须休息 R (1 ≤ R ≤ N) 个小时才能下次挤奶。给定Farmer John 计划的时间段,请你算出在 N 个小时内,最大的挤奶的量。

Input

1行三个整数NMR.接下来M行,每行三个整数SiEiPi

Output

 最大产奶量.

Sample Input

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

Sample Output

43

HINT

注意:结束时间不挤奶

 
网上都是按照牛来划分的阶段,我是按照时间来划分的
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; 
char buf[10000000], *ptr = buf - 1;
inline int readint(){
    int n = 0;
    char ch = *++ptr;
    while(ch < '0' || ch > '9') ch = *++ptr;
    while(ch <= '9' && ch >= '0'){
        n = (n << 1) + (n << 3) + ch - '0';
        ch = *++ptr;
    }
    return n;
}
const int maxn = 1000000 + 10, maxm = 1000 + 10;
struct Node{
    int s, e, p;
    Node(){}
    Node(int _s, int _e, int _p): s(_s), e(_e), p(_p){}
    bool operator < (const Node &x) const {
        return e < x.e;
    }
}a[maxm];
int dp[maxn] = {0};
int main(){
    freopen("in.txt", "r", stdin);
    fread(buf, sizeof(char), sizeof(buf), stdin);
    int N, M, R;
    N = readint();
    M = readint();
    R = readint();
    for(int i = 1; i <= M; i++){
        a[i].s = readint();
        a[i].e = readint();
        a[i].p = readint();
    }
    sort(a + 1, a + M + 1);
    int time = 0, ans = 0;
    for(int i = 1; i <= M; i++){
        while(time <= a[i].s){
            dp[time + 1] = max(dp[time], dp[time + 1]);
            time++;
        }
        if(a[i].e + R >= N)
            ans = max(ans, dp[a[i].s] + a[i].p);
        else dp[a[i].e + R] = max(dp[a[i].e + R], dp[a[i].s] + a[i].p);
    }
    for(int i = 0; i < N; i++)
        ans = max(ans, dp[i]);
    printf("%d\n", ans);
    return 0;
};

 

posted @ 2017-09-13 21:42  Elder_Giang  阅读(226)  评论(0编辑  收藏  举报