Gone Fishing
分类:枚举、贪心
1.题意概述
- n个池塘,从第i到第i+1个池塘需要 5t[i] 分钟,第i个池塘第一次捕鱼可以捕到f[i]条鱼,之后每五分钟减少d[i]条(如果池塘中鱼的数量已经小于d[i]则减少到0条),起点在第1个池塘,问h小时内最多可以捕多少鱼?
2.解题思路
- 由于John可以在任意的一个湖结束钓鱼,因此可以枚举他结束钓鱼的湖,求出相应情形下的最优方案然后再这些最优方案中选择最优方案即可:
- 假设他在湖x结束钓鱼,则用于钓鱼的时间为 Tx=60h−5ti(i=1、2、3...n) 分钟,在下一个 ti 内考虑John钓鱼,可以认为从一个湖u到另一个湖v所用的时间为0。即在任意一个时刻有可以从编号为1、2、3….x的湖中任选一个钓5分钟的鱼(意思即不考虑跑路的时间,先不断的在 x 个湖中选择当前钓鱼量最多的湖钓鱼)。
- 贪心的证明:对于每个湖来说,任何时候钓鱼量只和在这个湖钓鱼的次数有关,和总次数没关系,所以可用贪心思想求解(意思即最后再来考虑时间)
3.AC代码
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define lson root << 1
#define rson root << 1 | 1
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
int f[26], d[26], t[26], ans[26], tmp[26], tmp_f[26];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int T, kase = 0;
scanf("%d", &T);
while (T--)
{
printf("Case %d:\n", ++kase);
int n, h;
scanf("%d%d", &n, &h);
for (int i = 1; i <= n; i++)
scanf("%d", &f[i]);
for (int i = 1; i <= n; i++)
scanf("%d", &d[i]);
h *= 60;
t[1] = 0;
for (int i = 2; i <= n; i++)
{
int x;
scanf("%d", &x);
t[i] = t[i - 1] + x * 5;
}
fill(ans, ans + n + 1, 0);
int sum_max = 0;
for (int i = 1; i <= n; i++)
{
int sum = 0, res = h - t[i];
for (int j = 1; j <= i; j++)
tmp_f[j] = f[j];
fill(tmp, tmp + n + 1, 0);
while (res > 0)
{
int pos = -1, pos_max = 0;
for (int j = 1; j <= i; j++)
if (tmp_f[j] > pos_max)
{
pos = j;
pos_max = tmp_f[j];
}
if (pos == -1)
break;
sum += pos_max;
tmp[pos] += 5;
tmp_f[pos] -= d[pos];
res -= 5;
}
if (res > 0)
tmp[1] += res;
if (sum > sum_max)
{
sum_max = sum;
for (int j = 1; j <= i; j++)
ans[j] = tmp[j];
}
}
for (int i = 1; i <= n; i++)
if (i == 1)
printf("%d", ans[i]);
else
printf(", %d", ans[i]);
printf("\nNumber of fish expected: %d\n", sum_max);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}