zoukankan      html  css  js  c++  java
  • POJ 3616 Milking Time

    Milking Time

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 15   Accepted Submission(s) : 5
    Problem 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
    <p>* Line 1: Three space-separated integers: <i>N</i>, <i>M</i>, and <i>R</i><br>* Lines 2..<i>M</i>+1: Line <i>i</i>+1 describes FJ's ith milking interval withthree space-separated integers: <i>starting_hour<sub>i</sub></i> , <i>ending_hour<sub>i</sub></i> , and <i>efficiency<sub>i</sub></i> </p>
     

    Output
    <p>* Line 1: The maximum number of gallons of milk that Bessie can product in the <i>N</i> hours</p>
     

    Sample Input
    12 4 2 1 2 8 10 12 19 3 6 24 7 10 31
     

    Sample Output
    43
     

    Source
    PKU
     
    给奶牛挤奶,共m次可以挤,给出每次开始挤奶的时间st,结束挤奶的时间ed,还有挤奶的量ef,每次挤完奶要休息r时间,问最大挤奶量
     
    我的代码:dp[j] = max(dp[j], dp[a[i].s] + a[i].c);
     
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <string>
     5 #include <cstdio>
     6 using namespace std;
     7 long long dp[1000005];
     8 struct node
     9 {
    10     long long s, e, c;
    11 };
    12 bool cmp(node x, node y)
    13 {
    14     if (x.e == y.e)
    15     {
    16         if (x.c == y.c)
    17         {
    18             return x.s < y.s;
    19         }
    20         return x.c > y.c;
    21     }
    22     return x.e < y.e;
    23 }
    24 node a[1000005];//这里也要开这么大,不知道为什么呢
    25 int main()
    26 {
    27     long long n, m, r;
    28     cin >> n >> m >> r;
    29     long long i;
    30     for (i = 1; i <= m; i++)
    31     {
    32         cin >> a[i].s >> a[i].e >> a[i].c;
    33         a[i].e = a[i].e + r;
    34     }
    35     sort(a + 1, a + 1 + m, cmp);
    36     memset(dp, 0, sizeof(dp));
    37     long long j;
    38     i = 1;
    39     for (j = 1;j <= n+r; j++)
    40     {
    41         dp[j] = dp[j - 1];
    42         if (i<=m)
    43         {
    44             if (j == a[i].e)
    45             {
    46                 while (a[i].e == j)
    47                 {
    48                     dp[j] = max(dp[j], dp[a[i].s] + a[i].c);
    49                     i++;
    50                     if (i > m) break;
    51                 }
    52             }
    53         }
    54     }
    55     ////for (int i = 1; i <= m; i++)
    56     ////{
    57     ////    dp[i] = a[i].c;
    58     ////    for (int j = 1; j<i; j++)
    59     ////    {
    60     ////        if (a[j].e <= a[i].s)
    61     ////        {
    62     ////            dp[i] = max(dp[i], dp[j] + a[i].c);
    63     ////        }
    64 
    65     ////    }
    66     ////}
    67     long long mm = 0;
    68     for (i = 1; i <= m; i++)
    69     {
    70         if (dp[a[i].e] > mm)  mm = dp[a[i].e];
    71     }
    72     cout << mm << endl;
    73     return 0;
    74 }
    View Code
     
     
     
    更简单的代码:

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

    先把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 <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <string>
     5 using namespace std;
     6 int dp[1000005];
     7 struct node
     8 {
     9     int s, e, c;
    10 };
    11 bool cmp(node x, node y)
    12 {
    13     if (x.e == y.e)
    14     {
    15         if (x.c == y.c)
    16             return x.s < y.s;
    17         return x.c > y.c;
    18     }
    19     return x.e < y.e;
    20 }
    21 int main()
    22 {
    23     int n, m, r;
    24     cin >>n >> m >> r;
    25     node a[1005];
    26     int i;
    27     for (i = 1; i <= m; i++)
    28     {
    29         cin >> a[i].s >> a[i].e >> a[i].c;
    30         a[i].e = a[i].e + r;
    31     }
    32     sort(a + 1, a + 1 + m, cmp);
    33     memset(dp, 0, sizeof(dp));
    34     int j;
    35     i = 1;
    36     //for (j = 1;j <= n; j++)
    37     //{
    38     //    if (i<=m&&j == a[i].e)
    39     //    {
    40     //        while (i<=m&&a[i].e == j)
    41     //        {
    42     //            if (a[i].s - r < 0)
    43     //            {
    44     //                dp[j] = max(a[i].c, dp[a[i].e]);
    45     //            }
    46     //            dp[j] = max(dp[j], dp[a[i].s - r] + a[i].c);
    47     //            i++;
    48     //        }
    49     //    }
    50     //    else
    51     //    {
    52     //        dp[j] = dp[j - 1];
    53     //    }
    54     //}
    55     for (int i = 1; i <= m; i++)
    56     {
    57         dp[i] = a[i].c;
    58         for (int j = 1; j<i; j++)
    59         {
    60             if (a[j].e <= a[i].s)
    61             {
    62                 dp[i] = max(dp[i], dp[j] + a[i].c);
    63             }
    64 
    65         }
    66     }
    67     int ans = dp[1];
    68     for (int i = 2; i <= m; i++) ans = max(ans,dp[i]);
    69     cout << ans << endl;
    70     //cout << dp[n] << endl;
    71     return 0;
    72 }
    View Code
  • 相关阅读:
    0429 Scrum团队成立与第6-7章读后感
    0428 团队2.0
    0422 寻找数学口袋精灵BUG
    0422 Step2-FCFS调度
    0415 博客评价
    0414 结对--软件再升级(韩麒麟 列志华)
    0408 结对做汉堡
    0406 复利计算器--结对 组员 韩麒麟 列志华
    0405 构建之法第4章 读后感
    文法分析
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8433501.html
Copyright © 2011-2022 走看看