Uva 757 Gone Fishing

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=698
题意&题解:经典的贪心问题,刘汝佳小黑上的例题

需要注意的输出每个样例之间有空行,最后没有还有当结果为0的时候输出特殊处理

代码:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int MAX=30;
int d[MAX],t[MAX],f[MAX];
int fish[MAX],Time[MAX];
int anstime[MAX];
int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    int h,n;
    int tcase=0;
    while(scanf("%d",&n)&&n)
    {
        if(tcase)printf("\n");
        tcase++;
        scanf("%d",&h);
        h=h*12;
        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=1; i<n; i++)
        {
            scanf("%d",&t[i]);
            t[i]+=t[i-1];
        }
        int ans=0;
        memset(anstime,0,sizeof(anstime));
        for(int x=0; x<n; x++)
        {
            int rest=h-t[x];
            if(rest<=0) break;
            memset(Time,0,sizeof(Time));
            memcpy(fish,f,sizeof(fish));
            int tmpans=0;
            while(1)
            {
                if(rest<=0) break;
                int mmax=0,mpos=0;
                for(int i=0; i<=x; i++)
                {
                    if(mmax<fish[i])
                    {
                        mmax=fish[i];
                        mpos=i;
                    }
                }
                if(mmax==0) break;
                tmpans+=mmax;
                rest--;
                Time[mpos]++;
                fish[mpos]-=d[mpos];
            }
            if(rest>0) Time[0]+=rest;
            if(tmpans>ans)
            {
                ans=tmpans;
                memcpy(anstime,Time,sizeof(anstime));
            }
        }
        if(!ans) anstime[0]=h;//结果为0
        for(int i=0;i<n;i++)
        {
            printf("%d",anstime[i]*5);
            if(i!=n-1) printf(", ");
            else printf("\n");
        }
        printf("Number of fish expected: %d\n",ans);
    }
    return 0;
}
/*
4
1
0 0 0 0
0 0 0 0
1 2 3
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值