POJ 1042 Gone Fishing 贪心 枚举

来源:互联网 发布:北京网库 知乎 编辑:程序博客网 时间:2024/05/27 09:46

Gone Fishing
Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu
Submit

Status

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
Source
East Central North America 1999

思路:是按黑书上给出的思路编的,主要是贪心+枚举。因为每个池塘只能走一次,可以枚举前123,...n个池塘之间所能钓的最大鱼数。我们可以将路程一次性处理完毕,这样就可以瞬移..每次瞬移到鱼最多的池塘钓鱼就好。注意:所有池塘鱼为0;不需要走路就可以到下一个池塘;在某种状态下,这一个状态的最大鱼数等于前面的状态中的最大鱼数,这时候要考虑这种状态下是否有某一个池塘的编号小于前一个池塘或者,是不是同一编号下,这种状态下的那个池塘比前面状态下鱼数要大..可能没有说清楚,具体看代码1:钓鱼次数大于0才进去循环,而不是!=02:当每次求得的sum和最终结果ans相比时,大于ans直接复制,如果等于的话则一定要比较哪个结果中编号小的池塘呆的时间多。3:当鱼的数目为负数时,直接将数目变成0,而不是负数4:如果你是用排序的方法确定当前的最大值,最好自己写一个排序算法,因为你必须保证当前选的池塘的鱼的数目不仅是最多的,而且池塘的编号也要是最小的!!!直接用排序算法模板相当危险,我把插入排序改进了,重点是可能有很多值相等的情况,你必须保证你选择的池塘是编号最小的。虽说你可以每次从头搜索,找一个最大的,但会超时。5:最终结果ans的初值应该是负数,因为很可能、不,是就是会有最终结果为0的情况,所以利用if(sum>ans)判断结果时,ans的初值要是负数6:数据很变态很无耻,有di[i]=0的情况,鱼的数目不会减少;也有时间等于0的情况;显然也有f[i],t[i]=0的情况,好好想想是否都考虑到了,用数据测试一下就好了7:输出格式算是个小问题吧,记得有两个回车,最后一个池塘木有逗号。#include<stdio.h>#include<string>#include<cstring>#include<queue>#include<algorithm>#include<functional>#include<vector>#include<iomanip>#include<math.h>#include<iostream>#include<sstream>#include<stack>#include<set>#include<bitset>using namespace std;struct Ss{    int num,sum,d;    friend bool operator< (const Ss a,const Ss b)    {        if(a.sum<b.sum)            return true;        else if(a.sum==b.sum&&a.num>b.num)            return true;        else            return false;    }};struct St{    int sum,d;} s[50];int a[50],b[50],t[50];int main(){    int n,h;    while(scanf("%d",&n)>0&&n)    {        int tol=-10,tol1=0;        memset(a,0,sizeof(a));        scanf("%d",&h);        h=h*12;        for(int i=1; i<=n; i++)        {            scanf("%d",&s[i].sum);            if(s[i].sum<0)                s[i].sum=0;        }        for(int i=1; i<=n; i++)            scanf("%d",&s[i].d);        t[0]=t[1]=0;        for(int i=2; i<=n; i++)            scanf("%d",&t[i]);        for(int i=1; i<=n; i++)        {            priority_queue<Ss> q;            int ht=h;            tol1=0;            memset(b,0,sizeof(b));            for(int j=1; j<=i; j++)            {                Ss node;                node.num=j;                node.sum=s[j].sum;                node.d=s[j].d;                ht=ht-t[j];                if(node.sum>0)                    q.push(node);            }            while(!q.empty()&&ht>0)            {                Ss node=q.top();                q.pop();                tol1+=node.sum;                node.sum-=node.d;                b[node.num]++;                ht--;                if(node.sum>0)                {                    q.push(node);                }            }            if(tol1>=tol)            {                if(tol1>tol)                {                    tol=tol1;                    if(ht>0)                        b[1]+=ht;                    for(int k=1; k<=n; k++)                    {                        a[k]=b[k];                    }                }                else                {                    if(ht>0)                        b[1]+=ht;                    for(int k=1; k<=n; k++)                    {                        if(a[k]>b[k])                            break;                        else if(a[k]<b[k])                        {                            for(int j=1; j<=n; j++)                                a[j]=b[j];                            break;                        }                    }                }            }        }        for(int i=1; i<n; i++)            printf("%d, ",a[i]*5);        printf("%d\n",a[n]*5);        printf("Number of fish expected: %d\n\n",tol);    }    return 0;}
0 0
原创粉丝点击