UVa 757 - Gone Fishing

  题目大意:John有h的时间可以去钓鱼,有n湖可钓鱼,以5分钟为时间单位,每个湖初始每个单位时间可钓f条鱼,每下一个时间单位数量减少d条。同时,John只能从1号湖往后走进行钓鱼,湖之间的距离需要t个单位时间。求最多能钓到最多少鱼,并给出方案。

  “贪心+暴力,从近至远依次枚举可以到达的钓鱼地点,用总时间减去中途消耗的时间,这样就可以将其当成在各个钓鱼地点之间随意转移(实际题目中给出是单方向前行的),然后在已到达的最远的钓鱼地点之内的所有钓鱼地点上按每次能钓到的鱼的最大值做贪心,若时间用不完,则剩余时间加到第一个钓鱼地点上。注意,钓鱼地点最初的钓鱼值可能为零。”

 #include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define MAXN 30 struct Lake
{
int id, f;
bool operator < (const Lake& l) const
{
if (f != l.f) return f < l.f;
else return id > l.id;
}
}; int f[MAXN], d[MAXN], t[MAXN], fish[MAXN], res[MAXN]; int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int n;
bool first = true;
while (scanf("%d", &n) && n)
{
int h;
scanf("%d", &h);
for (int i = ; i < n; i++)
scanf("%d", &f[i]);
for (int i = ; i < n; i++)
scanf("%d", &d[i]);
t[] = ;
for (int i = ; i < n; i++)
{
scanf("%d", &t[i]);
t[i] += t[i-];
}
int ans = -;
for (int i = ; i < n; i++) // the last lake arrived
{
int time = h * ;
if (t[i] > time) break;
time -= t[i];
priority_queue<Lake> q;
Lake tmp;
for (int j = ; j <= i; j++)
if (f[j])
q.push( (Lake){j, f[j]} );
memset(fish, , sizeof(fish));
int sum = ;
while (!q.empty() && time > )
{
tmp = q.top();
q.pop();
sum += tmp.f;
int id = tmp.id;
fish[id]++;
time--;
tmp.f -= d[id];
if (tmp.f > ) q.push(tmp);
}
fish[] += time;
if (sum > ans)
{
ans = sum;
memcpy(res, fish, sizeof(fish));
}
}
if (first) first = false;
else printf("\n");
for (int i = ; i < n; i++)
printf("%d%s", res[i]*, (i==n-) ? "\n" : ", ");
printf("Number of fish expected: %d\n", ans);
}
return ;
}
上一篇:WebBrowser处理AJAX生成的网页内容!


下一篇:asp.net 未能写入输出文件--“拒绝访问”的解决办法