1. 程式人生 > >POJ 3616 Milking Time(加掩飾的LIS)

POJ 3616 Milking Time(加掩飾的LIS)

interval targe rest style pri scanf cep oss before

傳送門:

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

USACO 2007 November Silver 題目意思: 奶牛為自己規劃下面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)