// PA 2024 3B - Zelki #include <iostream> #include <string> #include <vector> #include <algorithm> #include <unordered_map> using namespace std; struct Single { int id, color, weight, price; Single(int id, int a, int b, int c) { this->id = id; this->color = a; this->weight = b; this->price = c; } }; void generateInitialResults( vector<vector<Single>> *jelliesByColor, vector<vector<long long>> *results, int m, int depth, int maxDepth) { for (int depth = 0; depth < maxDepth; ++depth) { for (unsigned i = 0; i < (*jelliesByColor)[depth].size(); ++i) { if (depth == 0) { if ((*results)[depth][(*jelliesByColor)[depth][i].weight % m] < 0 || (*results)[depth][(*jelliesByColor)[depth][i].weight % m] > (*jelliesByColor)[depth][i].price) { (*results)[depth][(*jelliesByColor)[depth][i].weight % m] = (*jelliesByColor)[depth][i].price; } } else { for (int j = 0; j < m; ++j) { if ((*results)[depth - 1][j] < 0) { continue; } if ((*results)[depth][(j +(*jelliesByColor)[depth][i].weight) % m] < 0 || (*results)[depth][(j + (*jelliesByColor)[depth][i].weight) % m] > (*jelliesByColor)[depth][i].price + (*results)[depth - 1][j]) { (*results)[depth][(j + (*jelliesByColor)[depth][i].weight) % m] = (*jelliesByColor)[depth][i].price + (*results)[depth - 1][j]; } } } } } } bool processResults( vector<long long> *results, int m, vector<bool> *toSkip) { bool somethingHappened = false; for (int i = 1; i < m; ++i) { if ((*toSkip)[i] || (*results)[i] < 0) { continue; } (*toSkip)[i] = true; for (int j = 1; j < m; ++j) { if ((*results)[j] > 0 && ((*results)[(i + j) % m] < 0 || (*results)[(i + j) % m] > (*results)[i] + (*results)[j])) { (*results)[(i + j) % m] = (*results)[i] + (*results)[j]; (*toSkip)[(i + j) % m] = false; somethingHappened = true; } } } return somethingHappened; } int main() { ios_base::sync_with_stdio(0); int n, k, m; int a, b, c; vector<vector<Single>> jelliesByColor; vector<vector<long long>> results; vector<bool> toSkip; cin >> n; cin >> k; cin >> m; for (int i = 0; i < k; ++i) { jelliesByColor.push_back(vector<Single>()); results.push_back(vector<long long>()); for (int j = 0; j < m; ++j) { results[i].push_back(-1); } } results[k - 1][0] = 0; for (int i = 0; i < m; ++i) { toSkip.push_back(true); } for (int i = 0; i < n; ++i) { cin >> a; cin >> b; cin >> c; --a; jelliesByColor[a].push_back(Single(i, a, b, c)); } generateInitialResults( &jelliesByColor, &results, m, 0, k); for (int i = 0; i < m; ++i) { if (results[k - 1][i] > 0) { toSkip[i] = false; } } while (processResults( &results[k - 1], m, &toSkip)) { } for (int i = 0; i < m; ++i) { cout << results[k - 1][i] << endl; } return 0; }
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | // PA 2024 3B - Zelki #include <iostream> #include <string> #include <vector> #include <algorithm> #include <unordered_map> using namespace std; struct Single { int id, color, weight, price; Single(int id, int a, int b, int c) { this->id = id; this->color = a; this->weight = b; this->price = c; } }; void generateInitialResults( vector<vector<Single>> *jelliesByColor, vector<vector<long long>> *results, int m, int depth, int maxDepth) { for (int depth = 0; depth < maxDepth; ++depth) { for (unsigned i = 0; i < (*jelliesByColor)[depth].size(); ++i) { if (depth == 0) { if ((*results)[depth][(*jelliesByColor)[depth][i].weight % m] < 0 || (*results)[depth][(*jelliesByColor)[depth][i].weight % m] > (*jelliesByColor)[depth][i].price) { (*results)[depth][(*jelliesByColor)[depth][i].weight % m] = (*jelliesByColor)[depth][i].price; } } else { for (int j = 0; j < m; ++j) { if ((*results)[depth - 1][j] < 0) { continue; } if ((*results)[depth][(j +(*jelliesByColor)[depth][i].weight) % m] < 0 || (*results)[depth][(j + (*jelliesByColor)[depth][i].weight) % m] > (*jelliesByColor)[depth][i].price + (*results)[depth - 1][j]) { (*results)[depth][(j + (*jelliesByColor)[depth][i].weight) % m] = (*jelliesByColor)[depth][i].price + (*results)[depth - 1][j]; } } } } } } bool processResults( vector<long long> *results, int m, vector<bool> *toSkip) { bool somethingHappened = false; for (int i = 1; i < m; ++i) { if ((*toSkip)[i] || (*results)[i] < 0) { continue; } (*toSkip)[i] = true; for (int j = 1; j < m; ++j) { if ((*results)[j] > 0 && ((*results)[(i + j) % m] < 0 || (*results)[(i + j) % m] > (*results)[i] + (*results)[j])) { (*results)[(i + j) % m] = (*results)[i] + (*results)[j]; (*toSkip)[(i + j) % m] = false; somethingHappened = true; } } } return somethingHappened; } int main() { ios_base::sync_with_stdio(0); int n, k, m; int a, b, c; vector<vector<Single>> jelliesByColor; vector<vector<long long>> results; vector<bool> toSkip; cin >> n; cin >> k; cin >> m; for (int i = 0; i < k; ++i) { jelliesByColor.push_back(vector<Single>()); results.push_back(vector<long long>()); for (int j = 0; j < m; ++j) { results[i].push_back(-1); } } results[k - 1][0] = 0; for (int i = 0; i < m; ++i) { toSkip.push_back(true); } for (int i = 0; i < n; ++i) { cin >> a; cin >> b; cin >> c; --a; jelliesByColor[a].push_back(Single(i, a, b, c)); } generateInitialResults( &jelliesByColor, &results, m, 0, k); for (int i = 0; i < m; ++i) { if (results[k - 1][i] > 0) { toSkip[i] = false; } } while (processResults( &results[k - 1], m, &toSkip)) { } for (int i = 0; i < m; ++i) { cout << results[k - 1][i] << endl; } return 0; } |