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;
    }
  • 相关阅读:
    一个数组中去除某一部分数组
    关于函数的同步异步
    多维数组转一维数组
    关于Promise的详细总结
    关于ES6(ES2015)的知识点详细总结
    vue实现一个会员卡的组件(可以动态传入图片(分出的一个组件)、背景、文字、卡号等)
    GitHub上常用命令(工作中几乎每天用到的命令)
    gitHub上如何设置或者取消电子邮箱提醒
    React评论展示案例(包含知识点:state、props、ref、React声明周期、localStorage本地存储等)
    感想2-对于组件化的一些思考
  • 原文地址:https://www.cnblogs.com/aerer/p/9930905.html
Copyright © 2011-2022 走看看