UVa 757 - Gone Fishing

链接

传送门

题意

\(n\)个鱼塘在一条线上,给出每个鱼塘最初钓鱼5分钟的收益和每钓5分钟收益的下降值,鱼塘间的距离。用\(h\)小时钓鱼,起点在1号鱼塘,输出收益最大的方案和最大收益,多解时要求输出编号小的鱼塘钓鱼时间尽量长的解。

思路

枚举最远走到第i个鱼塘,将走路的时间减去,然后用堆取每次钓鱼的最大收益。有个trick是鱼塘初始的5分钟钓鱼数量可能已经为0,不要放入堆。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;

const int maxn = 55;
int n, t;
int a[maxn][maxn], num[maxn], cnt[maxn];
int Max[maxn], p[maxn][maxn];
set<int> s;

void dfs(int d, int x) {
if (s.size() >= 10 && x >= *s.rbegin()) {
return;
}
if (d == n + 1) {
s.insert(x);
if (s.size() > 10) {
s.erase(*s.rbegin());
}
return;
}
for (int i = 0; i < cnt[d]; ++i) {
dfs(d + 1, x + p[d][i]);
}
}

int main() {
while (~scanf("%d", &n) && n) {
int ans = 0;
for (int i = 1; i <= n; ++i) {
scanf("%d", &num[i]);
Max[i] = 0, p[i][0] = 0, cnt[i] = 1;
for (int j = 1; j <= num[i]; ++j) {
scanf("%d", &a[i][j]);
a[i][j] = a[i][j - 1] + 10 - a[i][j];
if (a[i][j] > Max[i]) {
Max[i] = a[i][j];
cnt[i] = 0;
}
if (a[i][j] == Max[i]) {
p[i][cnt[i]++] = j;
}
}
ans += Max[i];
}
if (t) {
puts("");
}
printf("Workyards %d\n", ++t);
printf("Maximum profit is %d.\n", ans);
printf("Number of pruls to buy: ");
int Min = 0;
s.clear();
for (int i = 1; i <= n; ++i) {
if (cnt[i] > 0) {
Min += p[i][0];
}
for (int j = cnt[i] - 1; j >= 0; --j) {
p[i][j] -= p[i][0];
s.insert(p[i][j]);
}
}
while (s.size() > 10) {
s.erase(*s.rbegin());
}
dfs(1, 0);
printf("%d", Min);
s.erase(*s.begin());
for (auto it = s.begin(); it != s.end(); ++it) {
printf(" %d", Min + *it);
}
puts("");
}
return 0;
}