Milking Time

来源:互联网 发布:it是哪个国家 编辑:程序博客网 时间:2024/05/22 09:00

题目连接:https://cn.vjudge.net/contest/202335#problem/D

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

题意:输入n,m,r,分别代表母牛产奶的最大时间长度,产奶的时间段数,每次产完奶要休息的时间长度;

要求在时间允许的范围内母牛产奶量最高。

思路:1)先按照母牛产奶结束的时间由小到大排列好,然后用数组几下每个结束时间点的产奶量,之后再每段时间点允许的情况下选择合适的时间段来产奶。

2)将不同组合时间短下产的奶分装在不同的瓶中,设总共m瓶

3)mi[j].s>=mi[i].e+r&&dp[j]<dp[i]+mi[j].val  i段开始的时间点要小于j段产奶结束的时间点加休息的时间,并且用j瓶之前存储的总产奶量与i瓶总产奶量加j段的产奶量相比较;

4)最后在m个瓶中找最大的。

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct node{    int s;    int e;    int val;}mi[1005];int cmp(node a,node b)//按结束时间从小到大排列{    return a.e<b.e;}int main(){    int n,m,r;    scanf("%d %d %d",&n,&m,&r);    for(int i=0;i<m;i++)        scanf("%d %d %d",&mi[i].s,&mi[i].e,&mi[i].val);    sort(mi,mi+m,cmp);    int dp[m];    for(int i=0;i<m;i++)        dp[i]=mi[i].val;    for(int i=0;i<m;i++)    {        for(int j=i+1;j<m;j++)        {            if(mi[j].s>=mi[i].e+r&&dp[j]<dp[i]+mi[j].val)                dp[j]=dp[i]+mi[j].val;        }    }    int Max=0;    for(int i=0;i<m;i++)        if(Max<dp[i])            Max=dp[i];    printf("%d\n",Max);    return 0;}


原创粉丝点击