[poj] 1042 Gone Fishing

[poj] 1042 Gone Fishing

【题目】http://poj.org/problem?id=1042

【题意】求捕鱼最多的方案

【算法】贪心

【题解】枚举钓鱼的点,时间为t总-移动的时间t。那么时间钓鱼的总时间就是固定的了,然后贪心选出每次钓鱼次数最多的点进行钓鱼。时间复杂度为O(n*n),

【注意】这是一道细节题,Insert a blank line between cases.

【测试数据】

2
1
24 42
2 40
5

 

 

2
1
0 1
0 0
1

ans:
60, 0
Number of fish expected: 156

 

0, 55
Number of fish expected: 11

【AC代码】

#include<iostream>

#include<cstdio>

#include<cstring>

 

using namespace std;

 

int f[30],d[30],t[30],a[30],b[30],c[30],z[30];

 

int fun(int t, int n){

    int i,max,s=0;

    for(i=0;i<=n;i++){

        a[i]=f[i];

        b[i]=d[i];

        z[i]=0;

    }

    while(t-->0){

        max=0;

        //printf("%d\n",t);

        for(i=1;i<=n;i++)if(a[max]<a[i])max=i;

        if(a[max]<=0)break;

        s+=a[max];

        a[max]-=b[max];

        z[max]++;

    }

    return s;

}

 

int main(){

    int n,h,i,x,y;

    //int T=0;

    t[0]=0;

    while(scanf("%d",&n)&&n!=0){

       // if(T==0)T=1;

        //else putchar('\n');

        scanf("%d",&h);

        h*=12;

        for(i=0;i<n;i++){

            scanf("%d",&f[i]);

            c[i]=0;

        }

        for(i=0;i<n;i++)scanf("%d",&d[i]);

        for(i=1;i<n;i++)scanf("%d",&t[i]);

        x=0;

        c[0]=h*5;

        for(i=0;i<n&&h>0;i++){

           h-=t[i];

           //printf("%d\n",h/5);

           y=fun(h,i);

           if(y>x){

                x=y;

                c[0]=h*5;

                for(y=1;y<=i;y++){

                    c[0]-=(c[y]=z[y]*5);

                }

                for(;y!=0&&y<n;y++)c[y]=0;

           }

        }

        printf("%d",c[0]);

        for(i=1;i<n;i++)printf(", %d",c[i]);

        if(x<0)x=0;

        printf("\nNumber of fish expected: %d\n\n",x);

    }

    return 0;

}

【心得】我要坚持做黑书,我要坚持看英语。加油!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值