UVa 812 - Trade on Verweggistan

链接

传送门

题意

有$n$个bookyard,每个上面有$b_i$个pruls,价格不同,每个bookyard上的prul必须连续购买,每个买入的prul可以卖10 florins。输出可以获得最大收益的购买方案。

思路

求出前缀和,取每个书架前缀和的最大值即为最大收益。至于方案,dfs后保留10个最小值。

代码

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;
}