USACO Healthy Holsteins

Code

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
78
79
/*
ID: wcr19961
PROG: holstein
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn = 25;
const int maxm = 20;
int p[maxn], n, m;
int a[maxm][maxn];
bool better(int a, int b) {
int ca = __builtin_popcount(a), cb = __builtin_popcount(b);
if (ca < cb) {
return true;
}
if (ca > cb) {
return false;
}
for (int i = 0; i < m; ++i) {
if ((a & (1 << i)) != (b & (1 << i))) {
return a & (i << i);
}
}
return true;
}
int main() {
freopen("holstein.in", "r", stdin);
freopen("holstein.out", "w", stdout);
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &p[i]);
}
scanf("%d", &m);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
scanf("%d", &a[i][j]);
}
}
int S = 1 << m, ans = -1;
for (int i = 0; i < S; ++i) {
if (!better(i, ans)) {
continue;
}
bool ok = true;
for (int j = 0; j < n; ++j) {
int sum = 0;
for (int k = 0; k < m; ++k) {
if (i & (1 << k)) {
sum += a[k][j];
}
}
if (sum < p[j]) {
ok = false;
break;
}
}
if (ok) {
ans = i;
}
}
printf("%d", __builtin_popcount(ans));
for (int i = 0; i < m; ++i) {
if (ans & (1 << i)) {
printf(" %d", i + 1);
}
}
puts("");
return 0;
}

支付宝扫码领红包