zoukankan      html  css  js  c++  java
  • poj 3616

    Milking Time
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6599   Accepted: 2764

    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

    Source

     
     
    有三种设状态的方法
      第一种设dp[i][0]为时间到i时刚刚停止尚未休息的最大效率,dp[i][1]为时间到i时已经经过足够的时间休息的最大值,则
         不喂奶,让时间流走一分钟 dp[i + 1][1] = max(dp[i + 1][1], dp[i][1]);
      二分找到开始时间等于i的所有区间 dp[ a[k].t ][0] = max(dp[ a[k].t ][0], dp[i][1] + a[k].e);
      休息r时间 dp[i + r][1] = max(dp[i + r][1], dp[i][0]);
      
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 
     6 using namespace std;
     7 
     8 const int maxn = 1e6 + 7;
     9 typedef long long ll;
    10 int n, m, r;
    11 ll dp[maxn][2];
    12 struct node  {
    13     int s, t, e;
    14     bool operator < (const node &rhs) const {
    15         return s < rhs.s;
    16     }
    17 };
    18 
    19 node a[1005];
    20 
    21 int main() {
    22 
    23    // freopen("sw.in", "r", stdin);
    24     scanf("%d%d%d", &n, &m, &r);
    25     for (int i = 0; i < m; ++i) {
    26         scanf("%d%d%d", &a[i].s, &a[i].t, &a[i].e);
    27     }
    28 
    29 
    30     sort(a, a + m);
    31     memset(dp, -1, sizeof(dp));
    32     dp[0][1] = dp[0][0] = 0;
    33     for (int i = 0; i < n; ++i) {
    34         for (int st = 0; st <= 1; ++st) {
    35             if (st == 0) {
    36                 if (i + r <= n)
    37                 dp[i + r][1] = max(dp[i][st], dp[i + r][1]);
    38             } else {
    39                 dp[i + 1][1] = max(dp[i][st], dp[i + 1][1]);
    40                 int id = lower_bound(a, a + m, (node) {i, 0, 0}) - a;
    41                 while (id < m && a[id].s == i) {
    42                     dp[ a[id].t ][0] = max(dp[ a[id].t ][0], dp[i][st] + a[id].e);
    43                     id++;
    44                 }
    45             }
    46         }
    47     }
    48     ll ans = 0;
    49     for (int i = 0; i <= n; ++i) {
    50         //printf("dp[%d][0] = %d dp[%d][1] = %d 
    ", i, dp[i][0], i, dp[i][1]);
    51         ans = max(ans, dp[i][0]);
    52         ans = max(ans, dp[i][1]);
    53     }
    54 
    55     cout << ans << endl;
    56 //
    57 //    for (int i = 0; i < m; ++i) {
    58 //        printf("%d %d %d
    ", a[i].s, a[i].t, a[i].e);
    59 //    }
    60 
    61     return 0;
    62 }
    View Code

      上述的方法虽然可行但其实想复杂了,只需要设dp[i]为到i时间的最大效率值

      dp[i] = max(dp[i - 1], dp[ a[k].s - r ]+ a[k].e)即可

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 
     6 using namespace std;
     7 
     8 const int maxn = 1e6 + 7;
     9 typedef long long ll;
    10 int n, m, r;
    11 ll dp[maxn];
    12 struct node  {
    13     int s, t, e;
    14     bool operator < (const node &rhs) const {
    15         return t < rhs.t;
    16     }
    17 };
    18 
    19 node a[1005];
    20 
    21 int main() {
    22 
    23     //freopen("sw.in", "r", stdin);
    24     scanf("%d%d%d", &n, &m, &r);
    25     for (int i = 0; i < m; ++i) {
    26         scanf("%d%d%d", &a[i].s, &a[i].t, &a[i].e);
    27     }
    28 
    29 
    30     sort(a, a + m);
    31     memset(dp, 0, sizeof(dp));
    32 
    33     int k = 0;
    34     for (int T = 1; T <= n; ++T) {
    35         dp[T] = max(dp[T], dp[T - 1]);
    36         while (k < m && T >= a[k].t) {
    37             if (T != a[k].t) continue;
    38             if (a[k].s - r < 0) {
    39                 dp[T] = max(dp[T], (ll)a[k].e);
    40             } else {
    41                 dp[T] = max(dp[T], dp[ a[k].s - r] + a[k].e);
    42 
    43             }
    44             k++;
    45         }
    46     }
    47 
    48     ll ans = 0;
    49     for (int i = 0; i <= n; ++i) ans = max(ans, dp[i]);
    50     cout << ans << endl;
    51 
    52 //
    53 //    for (int i = 0; i < m; ++i) {
    54 //        printf("%d %d %d
    ", a[i].s, a[i].t, a[i].e);
    55 //    }
    56 
    57     return 0;
    58 }
    View Code

         其实还有一种最简单的做法,把区间按开始时间升序排列,如果开始时间相同则按结束时间,设dp[i]为从开始到第i的区间段的最大值

      dp[i] = max(dp[i], dp[j] + a[k].e);

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 
     6 using namespace std;
     7 
     8 const int maxn = 1005;
     9 typedef long long ll;
    10 int n, m, r;
    11 ll dp[maxn];
    12 struct node  {
    13     int s, t, e;
    14     bool operator < (const node &rhs) const {
    15         return s < rhs.s || (s == rhs.s && t < rhs.t) ;
    16     }
    17 };
    18 
    19 node a[1005];
    20 
    21 int main() {
    22 
    23     //freopen("sw.in", "r", stdin);
    24     scanf("%d%d%d", &n, &m, &r);
    25     for (int i = 0; i < m; ++i) {
    26         scanf("%d%d%d", &a[i].s, &a[i].t, &a[i].e);
    27     }
    28 
    29 
    30     sort(a, a + m);
    31     memset(dp, 0, sizeof(dp));
    32     for (int i = 0; i < m; ++i) dp[i] = a[i].e;
    33 
    34     for (int i = 1; i < m; ++i) {
    35         for (int j = 0; j < i; ++j) {
    36             if (a[j].t + r <= a[i].s) {
    37                 dp[i] = max(dp[i], dp[j] + a[i].e);
    38             }
    39         }
    40     }
    41 
    42     ll ans = 0;
    43     for (int i = 0; i < m; ++i) {
    44         ans = max(ans, dp[i]);
    45     }
    46 
    47     cout << ans << endl;
    48 
    49 //
    50 //    for (int i = 0; i < m; ++i) {
    51 //        printf("%d %d %d
    ", a[i].s, a[i].t, a[i].e);
    52 //    }
    53 
    54     return 0;
    55 }
    View Code
     
  • 相关阅读:
    微信小程序登录(包括获取不到unionid的情况)
    ionic生成签名的APK方法总结
    iframe的简单使用方法
    常见的浏览器端的存储技术:cookie
    AJAX 过程总结
    react相关知识总结2
    正则表达式相关知识点
    vue相关知识汇总
    react相关知识汇总
    Vue-Router核心实现原理
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/5122200.html
Copyright © 2011-2022 走看看