码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3616 Milking Time(加掩饰的LIS)

时间:2018-07-21 22:54:19      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:活动安排问题   arc   就是   possible   cow   must   put   end   each   

传送门:

http://poj.org/problem?id=3616

Milking Time
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13406   Accepted: 5655

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_houriN), an ending hour (starting_houri < ending_houriN), 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 ≤ RN) 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: N, M, 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

 
题目意思:
奶牛为自己规划下面n时间内的产奶,m个时间段,每个段有a,b,c表示从a时到b时共可产奶c。
挤奶工每次挤奶必须挤完完整的时间段,且每次挤完需要休息r时,求最终可获得的牛奶最大值
 
做法:
按结束时间(=结束时间+时间休息)排个序 贪心的思想,为什么是按照结束时间排序,具体请参考贪心经典样例之活动安排问题
为什么:是为了剩余时间最大化
 
    先按照贪心的想法按照结束时间(输入的结束时间+R)升序排序,
    然后用动态规划做。
    dp[i]代表第i个时间区间挤奶可获得的最大产奶量,
    需要遍历前i-1个时间区间中结束时间小于等于第i个时间区间中开始时间,
    求出最大值加上第i个时间区间的产奶量就是dp[i],
    最后去dp数组最大值即可。
 
code:
 
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define max_v 1005
struct node
{
    int s,f,v;
}p[max_v];
bool cmp(node a,node b)
{
    return a.f<b.f;
}
int dp[max_v];
int main()
{
    /*
    先按照贪心的想法按照结束时间(输入的结束时间+R)升序排序,
    然后用动态规划做。
    dp[i]代表第i个时间区间挤奶可获得的最大产奶量,
    需要遍历前i-1个时间区间中结束时间小于等于第i个时间区间中开始时间,
    求出最大值加上第i个时间区间的产奶量就是dp[i],
    最后去dp数组最大值即可。
    */
    int n,m,r;
    while(cin>>n>>m>>r)
    {
        for(int i=0;i<m;i++)
        {
            scanf("%d %d %d",&p[i].s,&p[i].f,&p[i].v);
            p[i].f+=r;
        }
        sort(p,p+m,cmp);
        for(int i=0;i<max_v;i++)
            dp[i]=0;
        dp[0]=p[0].v;
        int ans=dp[0];
        for(int i=1;i<m;i++)
        {
            int t=0;
            for(int j=0;j<i;j++)
            {
                if(p[j].f<=p[i].s)
                {
                    t=max(t,dp[j]);
                }
            }
            dp[i]=t+p[i].v;
            ans=max(ans,dp[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

POJ 3616 Milking Time(加掩饰的LIS)

标签:活动安排问题   arc   就是   possible   cow   must   put   end   each   

原文地址:https://www.cnblogs.com/yinbiao/p/9348097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!