POJ 1042 Gone Fishing(钓鱼问题)__贪心

##Description
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.

Input

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.

Output

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.

Sample Input

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

Sample Output

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

分析

题目大意:
Join可在h小时时间里在n个湖中钓鱼,5分钟为一单位时间,每个湖第一次钓鱼时可钓上的鱼为f(i), 每钓一个5分钟减少d(i)(不钓鱼则不再减少,这点很重要!),湖与湖之间行走时长为t(i),Join必须从第一个湖开始钓鱼,问怎样钓鱼才可使钓鱼量最大。

思路:
Join走路的时间是固定的,所以 h-走路时间 就是实际钓鱼的时间。枚举Join走的最后一个湖endLake,在[0, endLake]范围内每5分钟去查询可钓鱼最多的湖,然后去那里钓鱼,最终判断出在规定时间内以哪个湖为终点时可钓到的鱼的问题最大。
可能有人要问,找鱼最多的湖时不会重复来回多花走路时间吗?
这里要注意的是在一个湖中钓鱼5分钟后如果不再钓鱼的话下一个5分钟的可钓鱼量是不会随时间减少的,所以我们把每个5分钟内鱼最多的湖记下,最终可以统计出每个湖要停留几次,走的时间按顺序从0-endLake走即可。

实现

//在判断总钓鱼量相等时还是栽了。。

#include <iostream>
#include <cstring>
#define MAXN 26
int f[MAXN], currF[MAXN], d[MAXN], t[MAXN];  //当前可捕鱼数,鱼减少速率与到到下一个湖的时间。
int n, h;  //湖数目与捕鱼小时数。
int currAns[MAXN], ans[MAXN];
int maxFishCount = 0;

//当在湖中钓鱼的时间为remainTime并且endLake为钓鱼终点站时计算各个湖的钓鱼时长。
void solve(int remainTime, int endLake)
{
    int currFishCount = 0;
    memset(currAns, 0, sizeof(currAns));
    memcpy(currF, f, sizeof(f));
    for (int i = 0; i < remainTime; i++) {  //找每一单位时间都分配在哪个湖中钓鱼。
        int maxPos = 0;
        for (int j = 1; j <= endLake; j++) {    //遍历所有湖泊的剩鱼量,定位到当前可钓最多鱼的湖的位置。
            if (currF[maxPos] < currF[j]) { maxPos = j; }
        }
        currAns[maxPos]++;  //钓鱼次数+1
        currFishCount += currF[maxPos];
        currF[maxPos] = currF[maxPos] > d[maxPos] ? (currF[maxPos] - d[maxPos]) : 0;   //至多钓鱼钓到减少到没有鱼就不再钓了,最小为0。
    }
    if (currFishCount > maxFishCount) {
        maxFishCount = currFishCount;
        memcpy(ans, currAns, sizeof(currAns));
    }
    else if (currFishCount == maxFishCount) {  //如果最大可钓鱼量相等,则依据题目要求选排在前面的湖钓的多的那个方案。
        for (int i = 0; i <= endLake; i++) {
            if (currAns[i] != ans[i]) {
                if (currAns[i] > ans[i]) { memcpy(ans, currAns, sizeof(currAns)); }
                break;
            }
        }
    }
}

void print()
{
    for (int i = 0; i < n - 1; i++) {
        printf("%d, ", ans[i] * 5);
    }
    printf("%d\n", ans[n - 1] * 5);
    printf("Number of fish expected: %d\n\n", maxFishCount);
}

int main()
{
//  freopen("in.txt", "r", stdin);
    while (scanf("%d%d", &n, &h) && n) {
        for (int i = 0; i < n; i++) { scanf("%d", &f[i]); }
        for (int i = 0; i < n; i++) { scanf("%d", &d[i]); }
        for (int i = 0; i < n - 1; i++) { scanf("%d", &t[i]); }

        int remainTime = h * 60 / 5;  //每5分钟一单位时间。
        memset(ans, 0, sizeof(ans));
        maxFishCount = 0;
        for (int i = 0; i < n; i++) {
            solve(remainTime, i);
            remainTime -= t[i];  //减去赶路的时间。
        }
        print();
    }
    return 0;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值