zoukankan      html  css  js  c++  java
  • POJ_3616_Milking_Time_(动态规划)

    描述


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

    给奶牛挤奶,共m次可以挤,给出每次开始挤奶的时间st,结束挤奶的时间ed,还有挤奶的量ef,每次挤完奶要休息r时间,问最大挤奶量.

    Milking Time
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7507   Accepted: 3149

    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

    分析


    对于每一次挤奶,结束时间+=休息时间.

    先把m次挤奶按照开始时间排个序,用f[i]表示挤完第i个时间段的奶以后的最大挤奶量,那么有:

    f[i]=max(f[i],f[j]+(第i次挤奶.ef)) (1<=j<i&&(第j次挤奶).ed<=(第i次挤奶).st).

    注意:

    1.答案不是f[m]而是max(f[i]) (1<=i<=m) (因为不一定最后一次挤奶是哪一次).

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 
     5 const int maxm=1005;
     6 struct node
     7 {
     8     int st,ed,ef;
     9     bool operator < (const node &a) const
    10     {
    11         return a.st>st;
    12     }
    13 }a[maxm];
    14 int n,m,r;
    15 int f[maxm];
    16 
    17 void solve()
    18 {
    19     for(int i=1;i<=m;i++)
    20     {
    21         f[i]=a[i].ef;
    22         for(int j=1;j<i;j++)
    23         {
    24             if(a[j].ed<=a[i].st)
    25             {
    26                 f[i]=max(f[i],f[j]+a[i].ef);
    27             }
    28             
    29         }
    30     }
    31     int ans=f[1];
    32     for(int i=2;i<=m;i++) ans=max(ans,f[i]);
    33     printf("%d
    ",ans);
    34 }
    35 
    36 void init()
    37 {
    38     scanf("%d%d%d",&n,&m,&r);
    39     for(int i=1;i<=m;i++)
    40     {
    41         scanf("%d%d%d",&a[i].st,&a[i].ed,&a[i].ef);
    42         a[i].ed+=r;
    43     }
    44     sort(a+1,a+m+1);
    45 }
    46 
    47 int main()
    48 {
    49 #ifndef ONLINE_JUDGE
    50     freopen("milk.in","r",stdin);
    51     freopen("milk.out","w",stdout);
    52 #endif
    53     init();
    54     solve();
    55 #ifndef ONLINE_JUDGE
    56     fclose(stdin);
    57     fclose(stdout);
    58 #endif
    59     return 0;
    60 }
    View Code

     

     

  • 相关阅读:
    性能测试中的2-5-8原则
    Word2007页面排版
    博客备份专家--豆约翰
    TIOBE[ti'ɔbi]编程语言排行榜
    gif动画录制器LICEcap
    巧用输入法
    gitlab/github如何跳转页面
    Get和Post区别【boss面试集锦】
    带你了解直播流媒体
    直播+音视频行业相关
  • 原文地址:https://www.cnblogs.com/Sunnie69/p/5428210.html
Copyright © 2011-2022 走看看