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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| #include <vector> #include <algorithm> #include <iostream> #include <climits> using namespace std;
int N, B; char G; struct FM { int n; int no[100]; int value[100]; int cost[100]; int dp[1024][101]; bool select[1024][1024][101]; int limit; } mm, gg;
void zero_one_pack(struct FM& fm) { for (int i = 0; i <= B; i++) for (int j = 0; j <= fm.limit; j++) fm.dp[i][j] = -1000000000; fm.dp[0][0] = 0;
for (int i = fm.n - 1; i >= 0; i--) { for (int j = B; j >= fm.cost[i]; j--) { for (int k = fm.limit; k >= 1; k--) { int value = fm.dp[j - fm.cost[i]][k - 1] + fm.value[i]; if (value >= fm.dp[j][k]) { fm.dp[j][k] = value; fm.select[i][j][k] = true; } } } } }
vector<int> seq; bool get_result(struct FM &fm, int s, int k, int i) { if (s == 0 && k == 0) return true; for (; i < fm.n; i++) { if (fm.select[i][s][k]) { seq.push_back(fm.no[i]); if (get_result(fm, s - fm.cost[i], k - 1, i + 1)) return true; seq.pop_back(); } } }
vector<int> merge(vector<int> &a, vector<int> &b) { vector<int> result; int i = 0, j = 0; while (i < a.size() && j < b.size()) { if (a[i] < b[j]) { result.push_back(a[i++]); } else { result.push_back(b[j++]); } } while (i < a.size()) result.push_back(a[i++]); while (j < b.size()) result.push_back(b[j++]); return result; }
int main() { scanf("%d %d %d %d", &N, &gg.limit, &mm.limit, &B); for (int i = 1; i <= N; i++) { scanf("%c", &G); if (G == 'F') { int j = mm.n++; scanf("%d %d", &mm.value[j], &mm.cost[j]); mm.no[j] = i; } else if (G == 'M') { int j = gg.n++; scanf("%d %d", &gg.value[j], &gg.cost[j]); gg.no[j] = i; } else { i--; continue; } } zero_one_pack(mm); zero_one_pack(gg); int max_value = -1, min_cost = INT_MAX; vector<pair<int, int>> ijs; for (int i = 0; i <= B; i++) { for (int j = 0; j <= B - i; j++) { int value = gg.dp[i][gg.limit] + mm.dp[j][mm.limit]; if (value >= max_value) { if (value > max_value || i + j < min_cost) { max_value = value; min_cost = i + j; ijs.clear(); ijs.push_back(pair<int, int>(i, j)); } else if (i + j == min_cost) { ijs.push_back(pair<int, int>(i, j)); } } } } printf("%d %d\n", max_value, min_cost); vector<int> candidate(1, INT_MAX); for (auto &ij : ijs) { seq.clear(); get_result(gg, ij.first, gg.limit, 0); auto rgg = seq; seq.clear(); get_result(mm, ij.second, mm.limit, 0); auto rmm = seq; auto merged = merge(rgg, rmm); if (merged < candidate) candidate = merged; } for (int i = 0; i < candidate.size(); i++) { printf("%d%c", candidate[i], i == candidate.size() - 1 ? '\n' : ' '); } }
|