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

    Milking Time
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 3607   Accepted: 1533

    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: 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

     

    动态规划问题,用一个数组dp[i]记录到第i小时最大的产奶量

    先将所有牛的产奶时间按结束时间由小到大排序

    然后将i从0到n遍历递推一遍,每次先将dp[i]初始化为dp[i-1],然后遍历所有结束时间与i相等的奶牛

      dp[i]=max(dp[i],dp[time[t].start_hour-r]+time[t].eff)

    注意time[t].start_hour-r<0时,递推式是这样的:

      dp[i]=max(dp[i],time[t].eff)

     

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 
     6 using namespace std;
     7 
     8 typedef struct
     9 {
    10     int start_hour;
    11     int end_hour;
    12     int eff;
    13 } INTERVAL;
    14 
    15 int n,m,r;
    16 int dp[1000010];
    17 INTERVAL time[1010];
    18 
    19 bool cmp(INTERVAL a,INTERVAL b)
    20 {
    21     return a.end_hour<b.end_hour;
    22 }
    23 
    24 int main()
    25 {
    26     while(scanf("%d %d %d",&n,&m,&r)==3)
    27     {
    28         for(int i=0;i<m;i++)
    29             scanf("%d %d %d",&time[i].start_hour,&time[i].end_hour,&time[i].eff);
    30         sort(time,time+m,cmp);
    31 
    32         int t=0;
    33         dp[0]=0;
    34 
    35         for(int i=1;i<=n;i++)
    36         {
    37             dp[i]=dp[i-1];
    38             while(t<m&&time[t].end_hour==i)
    39             {
    40                 if(time[t].start_hour-r<0)
    41                     dp[i]=max(dp[i],time[t].eff);
    42                 else
    43                     dp[i]=max(dp[i],dp[time[t].start_hour-r]+time[t].eff);
    44                 t++;
    45             }
    46         }
    47         cout<<dp[n]<<endl;
    48     }
    49 
    50     return 0;
    51 }
    [C++]
  • 相关阅读:
    HTML5 中的Nav元素详解
    Gevent中信号量的使用
    MemCache缓存multiget hole详解
    MemCache中的内存管理详解
    Php中的强制转换详解
    Python中类的特殊方法详解
    MemCache的LRU删除机制详解
    AngularJS事件绑定的使用详解
    Php数据类型之整型详解
    HTML基础知识
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3273323.html
Copyright © 2011-2022 走看看