POJ 1042 Gone Fishing

POJ 1042 Gone Fishing(贪心)

Time Limit: 2000MS Memory Limit: 32768K
Total Submissions: 37615 Accepted: 11703

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 

题意

  有 n 个池塘,只能从第一个池塘开始走,可以在每个池塘中钓鱼,而且知道了每个池塘每五分钟钓鱼的数量都会下降一定额数值,且从池塘到下一个池塘之间都有一定的距离,知道了每个池塘走到下一个池塘的时间和每个池塘一开始能够钓鱼的数量,求在规定的时间内所能钓的最多的鱼的数量。

解题思路

  因为每个池塘的鱼并不是同时减少的,而是钓过鱼的池塘才会减少,所以没必要走回头路,这样可以直接把走路的时间减去,剩下的就是钓鱼的时间。同样根据每个池塘的鱼不是同时减少的这条性质,我们没必要在意整个钓鱼的过程,那么我们只需要知道,对于每5分钟哪个池塘的鱼最多就行了。所以贪心策略就已经很明显,即除去走路的时间,利用剩下的时间每次找剩余鱼最多的池塘即可,直到时间用完,最后的和就是最终答案。此外如果池塘没有鱼了,还有时间的话,把多余的时间分配给第一个池塘。

代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
const int maxn = 50;

int d[maxn],f[maxn],cff[maxn],t[maxn],cost[maxn][maxn],maxx[maxn];
int getmax(int n)
{
    int maxx=-1,pos=0;
    for(int i=0; i<n; i++)
    {
        if(maxx<cff[i])
        {
            maxx=cff[i];
            pos=i;
        }
    }
    return pos;
}
int main()
{
//    freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    int n;
    while(cin>>n&&n)
    {
        int h;
        cin>>h;
        h*=60;
        for(int i=0; i<n; i++)
            cin>>f[i];
        for(int i=0; i<n; i++)
            cin>>d[i];
        for(int i=1; i<n; i++)
            cin>>t[i];
        t[0]=0;
        memset(cost,0,sizeof(cost));
        memset(maxx,0,sizeof(maxx));
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
                cff[j]=f[j];
            int tmp=h;
            for(int j=0;j<=i;j++)
                tmp-=5*t[j];
            while(tmp>0)
            {
                tmp-=5;
                int cur=getmax(i+1);
                maxx[i]+=cff[cur];
                cost[i][cur]+=5;
                cff[cur]-=d[cur];
                if(cff[cur]<0) cff[cur]=0;
            }
        }
        int ans=0,pos=0;
        for(int i=0; i<n; i++)
        {
            if(ans<maxx[i])
            {
                pos=i;
                ans=maxx[i];
            }
        }
        for(int i=0; i<n; i++)
            printf("%s%d",i==0?"":", ",cost[pos][i]);
        printf("\n");
        printf("Number of fish expected: %d \n\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36258516/article/details/80215587
今日推荐