题目链接: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
*/