• POJ3616 Milking Time【dp】


    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

    思路:首先按照结束时间排序,dp[i]表示i时间挤奶的最大量,那么dp[i] = dp[i-1] + i时间挤奶量,仔细看代码推一推应该就会理解了。

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int N=1000005;
    const int M=1005;
    int a[N],dp[N];
    struct milk
    {
    	int start,end,value;
    }s[M];
    bool cmp(milk x,milk y)
    {
    	return x.end<y.end;
    }
    int main()
    {
    	int n,m,r;
    	scanf("%d%d%d",&n,&m,&r);
    	for(int i=1;i<=m;++i)
    		scanf("%d%d%d",&s[i].start,&s[i].end,&s[i].value);
    	sort(s+1,s+m+1,cmp);
    	memset(dp,0,sizeof(dp));
    	int maxn=0;
    	for(int i=1;i<=m;++i)
    	{
    		for(int j=1;j<i;++j)
    		{
    			if(s[j].end+r<=s[i].start)
    				dp[i]=max(dp[i],dp[j]);
    		}
    		dp[i]+=s[i].value;
    		maxn=max(maxn,dp[i]);
    	}
    	printf("%d
    ",maxn);
    	return 0;
    }
  • 相关阅读:
    一本通 1261:【例9.5】城市交通路网
    一本通 1263:【例9.7】友好城市(数据较弱)
    洛谷 P2983 [USACO10FEB]购买巧克力Chocolate Buying
    一本通 1266:【例9.10】机器分配
    【BZOJ5417】你的名字(NOI2018)-后缀自动机+主席树
    【BZOJ1396】识别子串-后缀自动机+线段树
    【HDU4787】GRE Words Revenge-AC自动机+分块
    【51Nod1766】树上的最远点对-线段树+树的直径
    【BZOJ3648】寝室管理-环套树+点分治+树状数组
    【BZOJ1367】Sequence(Baltic2004)-贪心+左偏树
  • 原文地址:https://www.cnblogs.com/aerer/p/9930905.html
Copyright © 2020-2023  润新知