Gone fishing
成绩 | 10 | 开启时间 | 2018年02月1日 星期四 20:25 |
折扣 | 0.8 | 折扣时间 | 2018年12月29日 星期六 20:25 |
允许迟交 | 否 | 关闭时间 | 2018年12月29日 星期六 20:25 |
Description
John is going on a fishing trip. He has h hours available (1 <= h <= 16), and there are n lakes in the area (2 <= n <= 25) all reachable along a single, one-way road. John starts at lake 1, but he can finish at any lake he wants. He can only travel from one lake to the next one, but he does not have to stop at any lake unless he wishes to. For each i = 1,...,n - 1, the number of 5-minute intervals it takes to travel from lake i to lake i + 1 is denoted ti (0 < ti <=192). For example, t3 = 4 means that it takes 20 minutes to travel from lake 3 to lake 4. To help plan his fishing trip, John has gathered some information about the lakes. For each lake i, the number of fish expected to be caught in the initial 5 minutes, denoted fi( fi >= 0 ), is known. Each 5 minutes of fishing decreases the number of fish expected to be caught in the next 5-minute interval by a constant rate of di (di >= 0). If the number of fish expected to be caught in an interval is less than or equal to di , there will be no more fish left in the lake in the next interval. To simplify the planning, John assumes that no one else will be fishing at the lakes to affect the number of fish he expects to catch.
Write a program to help John plan his fishing trip to maximize the number of fish expected to be caught. The number of minutes spent at each lake must be a multiple of 5.
Write a program to help John plan his fishing trip to maximize the number of fish expected to be caught. The number of minutes spent at each lake must be a multiple of 5.
Input
You will be given a number of cases in the input. Each case starts with a line containing n. This is followed by a line containing h. Next, there is a line of n integers specifying fi (1 <= i <=n), then a line of n integers di (1 <=i <=n), and finally, a line of n - 1 integers ti (1 <=i <=n - 1). Input is terminated by a case in which n = 0.
Output
For each test case, print the number of minutes spent at each lake, separated by commas, for the plan achieving the maximum number of fish expected to be caught (you should print the entire plan on one line even if it exceeds 80 characters). This is followed by a line containing the number of fish expected.
If multiple plans exist, choose the one that spends as long as possible at lake 1, even if no fish are expected to be caught in some intervals. If there is still a tie, choose the one that spends as long as possible at lake 2, and so on. Insert a blank line between cases.
If multiple plans exist, choose the one that spends as long as possible at lake 1, even if no fish are expected to be caught in some intervals. If there is still a tie, choose the one that spends as long as possible at lake 2, and so on. Insert a blank line between cases.
Sample Input
2 1 10 1 2 5 2 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
Sample Output
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
Source
East Central North America 1999
测试输入 | 期待的输出 | 时间限制 | 内存限制 | 额外进程 | |
---|---|---|---|---|---|
测试用例 1 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
写法一:用优先队列
#include<iostream>
#include<queue>
using namespace std;
struct node
{
int fish;
int mark;
friend bool operator < (node n1, node n2)
{
if (n1.fish != n2.fish)
return n1.fish<n2.fish;
else
return n1.mark>n2.mark;
}
}lakes[26];
int t[26];
int d[26];
int ans[26];
int h;
int n;
int cas;
int maxn;
int times;
int main()
{
//freopen("1.txt", "r", stdin);
//cin >> n >> h;
while (scanf("%d", &n) != -1)
{
if (n == 0) break;
cin >> h;
for (int i = 1; i <= n; i++)
{
cin >> lakes[i].fish;
lakes[i].mark = i;
}
maxn = 0;
for (int i = 1; i <= n; i++)
{
cin >> d[i];
}
for (int i = 1; i <= n - 1; i++)
{
cin >> t[i];
}
if (cas > 0)
cout << endl;
cas++;
for (int end = 1; end <= n; end++)
{
priority_queue <node> q;
times = h * 12;
int num = 0;
int tmp[26] = {0};
for (int i = 1; i <= end; i++) q.push(lakes[i]);
for (int i = 1; i<end; i++) times -= t[i];
while (times > 0)
{
node lake = q.top();
q.pop();
if (lake.fish > 0)
{
int lake_mark = lake.mark;
if (d[lake_mark] == 0)
{
num += lake.fish*times;
tmp[lake_mark] += times;
times = 0;
break;
}
times -= 1;
num += lake.fish;
lake.fish -= d[lake_mark];
tmp[lake_mark]++;
q.push(lake);
}
else break;
}
if (num > maxn)
{
maxn = num;
for (int i = 1; i <= n; i++) ans[i] = tmp[i];
if (times) ans[1] += times;
}
}
if (!maxn)ans[1] += h * 12;
for (int i = 1; i <= n; i++)
{
if (i == n)
cout << ans[i]*5;
else
cout << ans[i]*5 << ',' << ' ';
}
cout << endl;
cout << "Number of fish expected: " << maxn << endl;
}
return 0;
}
写法二:不用优先队列
#include<iostream>
#define Maxn 30
using namespace std;
int n, h;
int f[Maxn];
int d[Maxn];
int t[Maxn];
int ans[Maxn];
int cas;
int main()
{
//freopen("1.txt", "r", stdin);
while (~scanf("%d", &n) && n)
{
cin >> h;
h *= 12;
for (int i = 1; i <= n; i++)
cin >> f[i];
for (int i = 1; i <= n; i++)
cin >> d[i];
for (int i = 1; i < n; i++)
cin >> t[i];
int maxn = 0;
int times;
if (cas > 0)
cout << endl;
cas++;
for (int end = 1; end <= n; end++)//枚举终点
{
times = h;
int num = 0;//每次终点不同的鱼量
int tmp[Maxn] = { 0 };//临时存储不同终点情况下每个湖的鱼量
int ft[Maxn] = { 0 };//临时存储一下1~end湖中的鱼
for (int i = 1; i <= end; i++)
ft[i] = f[i];
for (int i = 1; i < end; i++)//先去除走路的时间
times -= t[i];
int k;
while (times>0)
{
k = 1;
for (int j = 1; j <= end; j++)
{
if (ft[j] > ft[k])
k = j;
}
if (ft[k] > 0)
{
num += ft[k];
ft[k] -= d[k];
tmp[k]++;
times--;
}
else break;
}
if (num > maxn)
{
maxn = num;
for (int i = 1; i <= n; i++) ans[i] = tmp[i];
if (times) ans[1] += times;
}
}
for (int i = 1; i <= n; i++)
{
if (i == n)
cout << ans[i] * 5;
else
cout << ans[i] * 5 << ',' << ' ';
}
cout << endl;
cout << "Number of fish expected: " << maxn << endl;
}
return 0;
}