一 原题
Burch & Kolstad
Farmer John prides himself on having the healthiest dairy cows in the world. He knows the vitamin content for one scoop of each feed type and the minimum daily vitamin requirement for the cows. Help Farmer John feed his cows so they stay healthy while minimizing the number of scoops that a cow is fed.
Given the daily requirements of each kind of vitamin that a cow needs, identify the smallest combination of scoops of feed a cow can be fed in order to meet at least the minimum vitamin requirements.
Vitamins are measured in integer units. Cows can be fed at most one scoop of any feed type. It is guaranteed that a solution exists for all contest input data.
PROGRAM NAME: holstein
INPUT FORMAT
Line 1: | integer V (1 <= V <= 25), the number of types of vitamins |
Line 2: | V integers (1 <= each one <= 1000), the minimum requirement for each of the V vitamins that a cow requires each day |
Line 3: | integer G (1 <= G <= 15), the number of types of feeds available |
Lines 4..G+3: | V integers (0 <= each one <= 1000), the amount of each vitamin that one scoop of this feed contains. The first line of these G lines describes feed #1; the second line describes feed #2; and so on. |
SAMPLE INPUT (file holstein.in)
4 100 200 300 400 3 50 50 50 50 200 300 200 300 900 150 389 399
OUTPUT FORMAT
The output is a single line of output that contains:
- the minimum number of scoops a cow must eat, followed by:
- a SORTED list (from smallest to largest) of the feed types the cow is given
SAMPLE OUTPUT (file holstein.out)
2 1 3
二 分析
三 代码
USER: Qi Shen [maxkibb3] TASK: holstein LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 4184 KB] Test 2: TEST OK [0.000 secs, 4184 KB] Test 3: TEST OK [0.000 secs, 4184 KB] Test 4: TEST OK [0.000 secs, 4184 KB] Test 5: TEST OK [0.000 secs, 4184 KB] Test 6: TEST OK [0.000 secs, 4184 KB] Test 7: TEST OK [0.000 secs, 4184 KB] Test 8: TEST OK [0.000 secs, 4184 KB] Test 9: TEST OK [0.000 secs, 4184 KB] Test 10: TEST OK [0.000 secs, 4184 KB] All tests OK.
Your program ('holstein') produced all correct answers! This is your submission #3 for this problem. Congratulations!
/*
ID:maxkibb3
LANG:C++
PROG:holstein
*/
#include<cstdio>
const int MAX_N = 25;
const int MAX_M = 15;
int n, m;
int target[MAX_N];
int feeds[MAX_M][MAX_N];
int current[MAX_N];
int trace[MAX_M], trace_size;
int ans[MAX_N];
void init() {
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &target[i]);
}
scanf("%d", &m);
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
scanf("%d", &feeds[i][j]);
}
}
}
bool enough() {
for(int i = 0; i < n; i++) {
if(current[i] < target[i]) return false;
}
return true;
}
bool judge(int left, int idx) {
if(enough()) return true;
if(left == 0) return false;
if(left + idx > m) return false;
if(judge(left, idx + 1)) {
return true;
}
for(int i = 0; i < n; i++) {
current[i] += feeds[idx][i];
}
trace[trace_size++] = idx;
if(judge(left - 1, idx + 1)) {
return true;
}
trace_size--;
for(int i = 0; i < n; i++) {
current[i] -= feeds[idx][i];
}
return false;
}
void solve() {
int l = 1, r = m;
bool first_eq = false;
while(l <= r) {
if(l == r) {
if(first_eq) break;
else first_eq = true;
}
int mid = (l + r) >> 1;
trace_size = 0;
for(int i = 0; i < n; i++) current[i] = 0;
if(judge(mid, 0)) {
r = mid;
for(int j = 0; j < trace_size; j++) {
ans[j] = trace[j];
}
}
else {
l = mid + 1;
}
}
printf("%d ", l);
for(int i = 0; i < l - 1; i++) {
printf("%d ", ans[i] + 1);
}
printf("%d\n", ans[l - 1] + 1);
}
int main() {
freopen("holstein.in", "r", stdin);
freopen("holstein.out", "w", stdout);
init();
solve();
return 0;
}