|
| 1 | +/* |
| 2 | +ID: jim_yub1 |
| 3 | +LANG: C++11 |
| 4 | +TASK: holstein |
| 5 | +*/ |
| 6 | + |
| 7 | +#include <iostream> |
| 8 | +#include <vector> |
| 9 | +#include <queue> |
| 10 | +#include <algorithm> |
| 11 | +#include <string> |
| 12 | + |
| 13 | +#define USACO |
| 14 | +#define FILEIN "holstein.in" |
| 15 | +#define FILEOUT "holstein.out" |
| 16 | + |
| 17 | +using namespace std; |
| 18 | + |
| 19 | +char letters[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
| 20 | + |
| 21 | +int getIndex(char n){ |
| 22 | + for (int i = 0; i < 16; i++) if (letters[i] == n) return i; |
| 23 | + return -1; |
| 24 | +} |
| 25 | + |
| 26 | +int main(){ |
| 27 | + #ifdef USACO |
| 28 | + freopen(FILEIN, "r", stdin); |
| 29 | + freopen(FILEOUT, "w", stdout); |
| 30 | + #endif |
| 31 | + |
| 32 | + |
| 33 | + int vitCount; |
| 34 | + cin >> vitCount; |
| 35 | + |
| 36 | + vector<int> req; |
| 37 | + for (int i = 0; i < vitCount; i++){ |
| 38 | + int n; |
| 39 | + cin >> n; |
| 40 | + |
| 41 | + req.push_back(n); |
| 42 | + } |
| 43 | + |
| 44 | + int solCount; |
| 45 | + cin >> solCount; |
| 46 | + |
| 47 | + vector<vector<int>> sol; |
| 48 | + |
| 49 | + for (int i = 0; i < solCount; i++){ |
| 50 | + vector<int> v; |
| 51 | + for (int j = 0; j < vitCount; j++){ |
| 52 | + int n; |
| 53 | + cin >> n; |
| 54 | + |
| 55 | + v.push_back(n); |
| 56 | + } |
| 57 | + sol.push_back(v); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + queue<string> search; |
| 62 | + search.push("1"); |
| 63 | + search.push("0"); |
| 64 | + |
| 65 | + vector<string> possible; |
| 66 | + |
| 67 | + while (!search.empty()){ |
| 68 | + string current = search.front(); |
| 69 | + search.pop(); |
| 70 | + |
| 71 | + if (current.length() == solCount){ |
| 72 | + string newStr = ""; |
| 73 | + |
| 74 | + for (int i = 0; i < current.length(); i++) |
| 75 | + if (current[i] == '1') newStr += letters[i]; |
| 76 | + |
| 77 | + possible.push_back(newStr); |
| 78 | + |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + if (current.length() > solCount) continue; |
| 83 | + |
| 84 | + search.push(current + "0"); |
| 85 | + search.push(current + "1"); |
| 86 | + } |
| 87 | + |
| 88 | + sort(possible.begin(), possible.end()); |
| 89 | + |
| 90 | + int minLength = 10000; |
| 91 | + |
| 92 | + vector<string> filt2; |
| 93 | + |
| 94 | + for (string s : possible){ |
| 95 | + int sum[30]; |
| 96 | + |
| 97 | + for (int i = 0; i < 30; i++) sum[i] = 0; |
| 98 | + |
| 99 | + for (int i = 0; i < s.length(); i++){ |
| 100 | + int solutionID = getIndex(s[i]); |
| 101 | + |
| 102 | + |
| 103 | + for (int j = 0; j < vitCount; j++) |
| 104 | + sum[j] += sol[solutionID][j]; |
| 105 | + } |
| 106 | + |
| 107 | + bool sat = true; |
| 108 | + |
| 109 | + for (int i = 0; i < vitCount; i++) |
| 110 | + if (sum[i] < req[i]){ |
| 111 | + sat = false; |
| 112 | + break; |
| 113 | + } |
| 114 | + |
| 115 | + if (sat){ |
| 116 | + minLength = min(minLength, (int)(s.length())); |
| 117 | + filt2.push_back(s); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + vector<string> filt; |
| 123 | + |
| 124 | + for (string s : filt2) if (s.length() == minLength) filt.push_back(s); |
| 125 | + |
| 126 | + |
| 127 | + sort(filt.begin(), filt.end()); |
| 128 | + |
| 129 | + cout << minLength; |
| 130 | + |
| 131 | + for (int i = 0; i < filt[0].length(); i++) |
| 132 | + cout << " " << getIndex(filt[0][i]) + 1; |
| 133 | + |
| 134 | + cout << endl; |
| 135 | + |
| 136 | + |
| 137 | + return 0; |
| 138 | +} |
0 commit comments