NYOJ 30 Gone Fishing(贪心)(个人理解笔记)

来源:互联网 发布:网络大电影题材 编辑:程序博客网 时间:2024/06/03 12:48

NYOJ 30 Gone Fishing(贪心)

时间限制:3000 ms | 内存限制:65535 KB


描述
John is going on a fishing trip. He has h hours available (1 <= h <= 16), and there are n lakes in the area (2 <= n <= 25) all reachable along a single, one-way road. John starts at lake 1, but he can finish at any lake he wants. He can only travel from one lake to the next one, but he does not have to stop at any lake unless he wishes to. For each i = 1,…,n - 1, the number of 5-minute intervals it takes to travel from lake i to lake i + 1 is denoted ti (0 < ti <=192). For example, t3 = 4 means that it takes 20 minutes to travel from lake 3 to lake 4. To help plan his fishing trip, John has gathered some information about the lakes. For each lake i, the number of fish expected to be caught in the initial 5 minutes, denoted fi( fi >= 0 ), is known. Each 5 minutes of fishing decreases the number of fish expected to be caught in the next 5-minute interval by a constant rate of di (di >= 0). If the number of fish expected to be caught in an interval is less than or equal to di , there will be no more fish left in the lake in the next interval. To simplify the planning, John assumes that no one else will be fishing at the lakes to affect the number of fish he expects to catch.
Write a program to help John plan his fishing trip to maximize the number of fish expected to be caught. The number of minutes spent at each lake must be a multiple of 5.


输入
You will be given a number of cases in the input. Each case starts with a line containing n. This is followed by a line containing h. Next, there is a line of n integers specifying fi (1 <= i <=n), then a line of n integers di (1 <=i <=n), and finally, a line of n - 1 integers ti (1 <=i <=n - 1). Input is terminated by a case in which n = 0.


输出
For each test case, print the number of minutes spent at each lake, separated by commas, for the plan achieving the maximum number of fish expected to be caught (you should print the entire plan on one line even if it exceeds 80 characters). This is followed by a line containing the number of fish expected.
If multiple plans exist, choose the one that spends as long as possible at lake 1, even if no fish are expected to be caught in some intervals. If there is still a tie, choose the one that spends as long as possible at lake 2, and so on. Insert a blank line between cases.


样例输入

2
1
10 1
2 5
2
4
4
10 15 20 17
0 3 4 3
1 2 3
4
4
10 15 50 30
0 3 4 3
1 2 3
0

样例输出

45, 5
Number of fish expected: 31

240, 0, 0, 0
Number of fish expected: 480

115, 10, 50, 35
Number of fish expected: 724

毕竟是菜逼,所以写一下看完别人代码后的理解。

题意:

John有n个小时去钓鱼,有1到n个湖有鱼可以钓,john从湖i到湖i+1所消耗的时间为time[i]*5,每个湖有鱼f[i],钓一次鱼消耗5分钟,湖中鱼的数量就去掉的d[i],如果某次钓鱼时湖中鱼的数量小于或等于d[i],那么钓完后湖i中就没有鱼了。目的是让john钓到的鱼最多。


分析:

显然在某个湖钓鱼与其他湖钓鱼互不影响,所以直接枚举john在1到i个湖的钓鱼范围,然后直接贪心钓,走路时间的话,只要算到走到第i个湖的时间,因为互不影响,所以可以在中途某个湖多钓几次再走到下个湖。


代码如下(参考了别人的):

#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;const int N = 30;int f[N],ans[N],d[N],tmp[N],time[N];int ff[N];int main(){    int ca = 1,h,n;    while(~scanf("%d",&n)&&n){        scanf("%d",&h);        h*=60;        for(int i=0;i<n;i++) scanf("%d",&f[i]);        for(int i=0;i<n;i++) scanf("%d",&d[i]);        time[0] = 0;        for(int i=1,t;i<n;i++){            scanf("%d",&t);            time[i] = time[i-1] + t*5; // 预处理到第i个湖的时间费用        }        memset(ans,0,sizeof(ans));        int maxsum = -1;        for(int i=0;i<n;i++){ // 枚举去钓鱼的范围,中途某些湖可能不会去钓            int sum =0 ,left_time = h - time[i]; //因为去某个湖钓鱼和去其他湖钓鱼是互不影响的,所以直接减去去到最远的湖所要时间就行,中途多钓几次然后去下面的湖钓            for(int j=0;j<=i;j++) ff[j]=f[j]; //防止影响其他枚举            memset(tmp,0,sizeof(tmp));            while(left_time>0){                int maxx =0 ,id=0;                for(int j=0;j<=i;j++){                    if(ff[j]>maxx){                        maxx = ff[j];                        id = j;                    }                }                if(maxx==0) break;                sum += maxx;                tmp[id] += 5;                ff[id] -= d[id];                left_time -= 5;            }            if(left_time>0) tmp[0] += left_time;            if(sum > maxsum){                maxsum = sum;                for(int j=0;j<=i;j++)                {                    ans[j] = tmp[j];                }            }        }        if(ca>1) printf("\n");        printf("%d",ans[0]);        for(int i=1;i<n;i++){            printf(", %d",ans[i]);        }        printf("\n");        printf("Number of fish expected: %d\n",maxsum);        ca++;    }    return 0;}

如果有冒犯,请原谅或者联系本菜。

1 0