您的位置:首页 > 编程语言 > Go语言

POJ 1042 Gone Fishing (枚举+贪心)

2013-02-04 14:28 375 查看
题目:http://poj.org/problem?id=1042

John去钓鱼,他有h个小时可以钓鱼(1 <= h <= 16),在一条路上,有n个湖 (2 <= n <= 25) ,John从湖1开始钓鱼,他可以在任意一个湖结束。他只能依次经过湖,可以选择不停留。从第i个湖到第i+1个湖需要t[i]*5分钟。每个湖在最初的5分钟钓到的鱼的条数的期望是f[i],每钓5分钟鱼的期望减少d[i]。如果期望<=d[i],则在下一轮中池中没有鱼。为使期望最大,求John在每个湖的停留时间,和期望值。

思路:枚举John在每一个湖停止钓鱼的最优解。先减去路上的耗时,然后优先钓鱼多的湖,直到时间耗尽。

这题WA了几次,有几个原因,首先是忘了interval 比n小1. 然后是输出的时候忘了逗号,最后是如果全部为0的情况没有考虑。

#include <iostream>
#include <cmath>
#include <cstring>
#include <stdio.h>

using namespace std;

int f[30],d[30],t[30];
int ans[30];
int main()
{
int line=0;

while(1)
{
int spendtime[30]={0};
int fishinlake[30]={0};
int ans[30]={0};
int ansexp=0;
line++;
int n;
int h;

cin>>n;
if(n==0) break;
cin>>h;
for (int i = 0; i < n; i++)
{
cin>>f[i];
}
for (int i = 0; i < n; i++)
{
cin>>d[i];
}
for (int i = 0; i < n-1; i++)
{
cin>>t[i];
}

int time=h*12;
ans[0]=time;
for (int x=0;x<n;x++)
{
for(int j=0;j<=x;j++)
{
fishinlake[j]=f[j];
spendtime[j]=0;
}

int fishtime=time;
for(int j=0;j<=x-1;j++)
{
fishtime-=t[j];
}
int exp=0;
int remaintime;
for(;fishtime>0;fishtime--)
{
int maxfish=fishinlake[0];
int maxi=0;
for(int k=0;k<n;k++)
{
if(maxfish<fishinlake[k])
{
maxfish=fishinlake[k];
maxi=k;
}
}
if(fishinlake[maxi]<=0)
{
remaintime=fishtime;
break;

}
exp+=fishinlake[maxi];
fishinlake[maxi]-=d[maxi];
spendtime[maxi]++;

}
if (exp>ansexp)
{
ansexp=exp;
for(int j=0;j<n;j++)
{
ans[j]=spendtime[j];
}
remaintime=fishtime;
ans[0]+=remaintime;
}
}
if(line!=1) printf("\n");
int first=1;
for(int i=0;i<n;i++)
{
if(first)
{
printf("%d",ans[i]*5);
first=0;
}
else printf(", %d",ans[i]*5);
}
printf("\nNumber of fish expected: %d \n",ansexp);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: