zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    LeetCode--Sudoku Solver
    LeetCode--Merge Intervals
    LeetCode--Valid Number
    LeetCode--Max Points on a Line
    1.1
    智能指针原理与简单实现(转)
    C++内存管理(转)
    算法题--扔棋子
    LeetCode--Substring with Concatenation of All Words
    线性代数与MATALB1
  • 原文地址:https://www.cnblogs.com/aerer/p/9930905.html
Copyright © 2011-2022 走看看