原题链接
之前WA了两次,都是由于没考虑到0的情况。
样例输入
2 湖的个数
1 小时数
10 1 最初5分钟捕鱼数
2 5 每5分钟减少的鱼数
2 跑路间隔时间*5 min
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
样例输出
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
//关键点:求出在第一个湖耗时最长的,所以如果有多余的时间则尽量都耗在第一个湖边
//贪心+枚举
#include <stdio.h>
#include <string.h>
int initialLakeFishes[26], fishReduce[26], moveTime[26];
int timeSpend[26], eachFish[26], totalFish[26], copyInitialLake[26];
struct Node{
int lake, fish;
} maxLake;
int findMaxLake(int n){
maxLake.lake = maxLake.fish = 0;
int i;
for(i = 0; i <= n; ++i){
if(copyInitialLake[i] > maxLake.fish)
maxLake.fish = copyInitialLake[i], maxLake.lake = i;
}
return maxLake.fish;
}
void mergeSum(int n){
int sum = 0;
for(int i = 0; i <= n; ++i)
sum += eachFish[i];
totalFish[n] = sum;
}
int findMaxTotal(int n){
int i, max = 0, j = 0;
for(i = 0; i <= n; ++i)
if(totalFish[i] > max)
max = totalFish[i], j = i;
return j;
}
int main(){
int lakeNumbers, hours;
int hoursToMinutes, copy_hoursToMinutes, i, n;
while(scanf("%d", &lakeNumbers), lakeNumbers){
scanf("%d", &hours);
hoursToMinutes = hours * 60;
copy_hoursToMinutes = hoursToMinutes;
for(i = 0; i != lakeNumbers; ++i)
scanf("%d", &initialLakeFishes[i]);
for(i = 0; i != lakeNumbers; ++i)
scanf("%d", &fishReduce[i]);
for(i = 0; i != lakeNumbers - 1; ++i)
scanf("%d", &moveTime[i]);
//从第一个湖开始枚举,假设小明最远到达第n个湖
//则小明可以在这n个湖之间“瞬移”。
for(n = 0; n != lakeNumbers; ++n){
memcpy(copyInitialLake, initialLakeFishes, sizeof(initialLakeFishes));
for(i = 0; i < n; ++i){
hoursToMinutes -= moveTime[i] * 5;
}
while(hoursToMinutes >= 5){
if(findMaxLake(n) == 0)
break;
timeSpend[maxLake.lake] += 5;
hoursToMinutes -= 5;
eachFish[maxLake.lake] += maxLake.fish;
copyInitialLake[maxLake.lake] -= fishReduce[maxLake.lake];
}
mergeSum(n);
memset(eachFish, 0, sizeof(eachFish));
memset(timeSpend, 0, sizeof(timeSpend));
hoursToMinutes = copy_hoursToMinutes;
}
memcpy(copyInitialLake, initialLakeFishes, sizeof(initialLakeFishes));
n = findMaxTotal(lakeNumbers - 1);
for(i = 0; i < n; ++i){
hoursToMinutes -= moveTime[i] * 5;
}
while(hoursToMinutes >= 5){
if(findMaxLake(n) == 0)
break;
timeSpend[maxLake.lake] += 5;
hoursToMinutes -= 5;
eachFish[maxLake.lake] += maxLake.fish;
copyInitialLake[maxLake.lake] -= fishReduce[maxLake.lake];
}
while(hoursToMinutes >= 5){
timeSpend[0] += 5;
hoursToMinutes -= 5;
}
printf("%d", timeSpend[0]);
for(i = 1; i != lakeNumbers; ++i)
printf(", %d", timeSpend[i]);
printf("\nNumber of fish expected: %d\n\n", totalFish[n]);
memset(totalFish, 0, sizeof(totalFish));
memset(eachFish, 0, sizeof(eachFish));
memset(timeSpend, 0, sizeof(timeSpend));
}
return 0;
}