#include <vector> #include <unordered_set> #include <algorithm> #include <iostream> struct Item { int color; int weight; int cost; }; struct Item2 { int color; int weight; int cost; int costDifferenceToNext; }; struct InputCheck { bool eachColorOnce; bool eachColorAtLeastOnce; }; class Solution { public: Solution(int aN, int aColors, int aWeight) : n(aN) , maxColor(aColors) , maxWeight(aWeight) {} void run() { const InputCheck inputCheck = getInput(); if (!inputCheck.eachColorAtLeastOnce) { std::cout << 0 << "\n"; for (int i = 1; i < maxWeight; i++) { std::cout << -1 << "\n"; } return; } if (inputCheck.eachColorOnce) { solveForEachColorOnce(); } else { solveGeneralCase(); } } InputCheck getInput() { InputCheck inputCheck{true, true}; std::unordered_set<int> usedColors; std::vector<bool> colorIsPresent(maxColor + 1, false); for (int i = 0; i < n; i++){ //int color, weight, cost; Item item; std::cin >> item.color >> item.weight >> item.cost; items.push_back(item); colorIsPresent[item.color] = true; if (usedColors.find(item.color) == usedColors.end()) { usedColors.insert(item.color); } else { inputCheck.eachColorOnce = false; } } for (int i = 1; i <= maxColor; i++) { if (!colorIsPresent[i]) { inputCheck.eachColorAtLeastOnce = false; break; } } return inputCheck; } void solveForEachColorOnce() { int count{1}; long long weightSum{0}; long long costSum{0}; std::vector<long long> individualSums(maxWeight, -1); individualSums[0] = 0; for (auto & item : items) { weightSum += item.weight; weightSum %= maxWeight; costSum += item.cost; } long long weightSumForOneCollection = weightSum; long long costSumForOneCollection = costSum; std::unordered_set<long long> usedWeights; usedWeights.insert(weightSum); while (count < maxWeight) { if (individualSums[weightSum] == -1 && weightSum != 0) { individualSums[weightSum] = costSum; count++; } weightSum += weightSumForOneCollection; weightSum %= maxWeight; costSum += costSumForOneCollection; if (usedWeights.find(weightSum) != usedWeights.end()) { break; } else { usedWeights.insert(weightSum); } } for (auto cost : individualSums) { std::cout << cost << "\n"; } } void solveGeneralCase() { std::sort(items.begin(), items.end(), [](const auto & left, const auto & right) { return left.cost < right.cost; }); std::vector<Item2> items2; std::vector<std::vector<Item2>> itemsByColor(maxColor + 1, std::vector<Item2>()); for (const auto & entry : items) { Item2 item2; item2.color = entry.color; item2.weight = entry.weight; item2.cost = entry.cost; item2.costDifferenceToNext = 1500000000; if (!itemsByColor[item2.color].empty()) { int costDifference = item2.cost - itemsByColor[item2.color].back().cost; itemsByColor[item2.color].back().costDifferenceToNext = costDifference; } itemsByColor[item2.color].push_back(item2); } for (auto & entryColor : itemsByColor) { if (!entryColor.empty()) { for (auto & entry : entryColor) { items2.push_back(entry); } } } std::sort(items2.begin(), items2.end(), [](const auto & left, const auto & right) { return left.costDifferenceToNext < right.costDifferenceToNext; }); // --------------- Searching for solution ------------------ std::vector<long long> individualSums(maxWeight, -1); individualSums[0] = 0; int count{1}; long long weightSum{0}; long long costSum{0}; // Get first entry to individualSums std::unordered_set<int> usedColors; for (auto entry : items2) { if (usedColors.find(entry.color) == usedColors.end()) { weightSum += entry.weight; weightSum %= maxWeight; costSum += entry.cost; usedColors.insert(entry.color); } } individualSums[weightSum] = costSum; // Get next entries based on using each color once for (int positionToSkip = 0; positionToSkip + 1 < items2.size(); positionToSkip++) { usedColors.clear(); weightSum = 0; costSum = 0; for (int i = 0; i < items2.size(); i++) { if (i == positionToSkip) { continue; } if (usedColors.find(items2[i].color) == usedColors.end()) { weightSum += items2[i].weight; weightSum %= maxWeight; costSum += items2[i].cost; usedColors.insert(items2[i].color); } } if (usedColors.size() == maxColor && (individualSums[weightSum] == -1 || individualSums[weightSum] > costSum)) { individualSums[weightSum] = costSum; } } for (int i = 2; i < 10; i++) { usedColors.clear(); weightSum = 0; costSum = 0; for (int colorId = 1; colorId < itemsByColor.size(); colorId++) { for (int j = 0; j < i; j++) { auto & item = itemsByColor[colorId][j % itemsByColor[colorId].size()]; weightSum += item.weight; weightSum %= maxWeight; costSum += item.cost; } } if (individualSums[weightSum] == -1 || individualSums[weightSum] > costSum) { individualSums[weightSum] = costSum; } } for (int colorA = 1; colorA <= maxColor; colorA++) { for (int colorB = 1; colorB <= maxColor; colorB++) { for (int colorC = 1; colorC <= maxColor; colorC++) { usedColors.clear(); weightSum = 0; costSum = 0; for (int colorId = 1; colorId < itemsByColor.size(); colorId++) { if (colorId == colorA) { auto & item = itemsByColor[colorId][0]; weightSum += item.weight; weightSum %= maxWeight; weightSum += item.weight; weightSum %= maxWeight; costSum += item.cost; costSum += item.cost; } else if (colorId == colorB) { auto & item = itemsByColor[colorId][0]; weightSum += item.weight; weightSum %= maxWeight; costSum += item.cost; auto & itemSecond = itemsByColor[colorId][(1 < itemsByColor[colorId].size() ? 1 : 0)]; weightSum += itemSecond.weight; weightSum %= maxWeight; costSum += itemSecond.cost; } else { auto & itemSecond = itemsByColor[colorId][(1 < itemsByColor[colorId].size() ? 1 : 0)]; weightSum += itemSecond.weight; weightSum %= maxWeight; costSum += itemSecond.cost; weightSum += itemSecond.weight; weightSum %= maxWeight; costSum += itemSecond.cost; } } if (individualSums[weightSum] == -1 || individualSums[weightSum] > costSum) { individualSums[weightSum] = costSum; } } } } for (auto cost : individualSums) { std::cout << cost << "\n"; } } private: int n; int maxColor; int maxWeight; std::vector<Item> items; }; int main() { int n, k, m; std::cin >> n >> k >> m; Solution solution(n, k, m); solution.run(); 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | #include <vector> #include <unordered_set> #include <algorithm> #include <iostream> struct Item { int color; int weight; int cost; }; struct Item2 { int color; int weight; int cost; int costDifferenceToNext; }; struct InputCheck { bool eachColorOnce; bool eachColorAtLeastOnce; }; class Solution { public: Solution(int aN, int aColors, int aWeight) : n(aN) , maxColor(aColors) , maxWeight(aWeight) {} void run() { const InputCheck inputCheck = getInput(); if (!inputCheck.eachColorAtLeastOnce) { std::cout << 0 << "\n"; for (int i = 1; i < maxWeight; i++) { std::cout << -1 << "\n"; } return; } if (inputCheck.eachColorOnce) { solveForEachColorOnce(); } else { solveGeneralCase(); } } InputCheck getInput() { InputCheck inputCheck{true, true}; std::unordered_set<int> usedColors; std::vector<bool> colorIsPresent(maxColor + 1, false); for (int i = 0; i < n; i++){ //int color, weight, cost; Item item; std::cin >> item.color >> item.weight >> item.cost; items.push_back(item); colorIsPresent[item.color] = true; if (usedColors.find(item.color) == usedColors.end()) { usedColors.insert(item.color); } else { inputCheck.eachColorOnce = false; } } for (int i = 1; i <= maxColor; i++) { if (!colorIsPresent[i]) { inputCheck.eachColorAtLeastOnce = false; break; } } return inputCheck; } void solveForEachColorOnce() { int count{1}; long long weightSum{0}; long long costSum{0}; std::vector<long long> individualSums(maxWeight, -1); individualSums[0] = 0; for (auto & item : items) { weightSum += item.weight; weightSum %= maxWeight; costSum += item.cost; } long long weightSumForOneCollection = weightSum; long long costSumForOneCollection = costSum; std::unordered_set<long long> usedWeights; usedWeights.insert(weightSum); while (count < maxWeight) { if (individualSums[weightSum] == -1 && weightSum != 0) { individualSums[weightSum] = costSum; count++; } weightSum += weightSumForOneCollection; weightSum %= maxWeight; costSum += costSumForOneCollection; if (usedWeights.find(weightSum) != usedWeights.end()) { break; } else { usedWeights.insert(weightSum); } } for (auto cost : individualSums) { std::cout << cost << "\n"; } } void solveGeneralCase() { std::sort(items.begin(), items.end(), [](const auto & left, const auto & right) { return left.cost < right.cost; }); std::vector<Item2> items2; std::vector<std::vector<Item2>> itemsByColor(maxColor + 1, std::vector<Item2>()); for (const auto & entry : items) { Item2 item2; item2.color = entry.color; item2.weight = entry.weight; item2.cost = entry.cost; item2.costDifferenceToNext = 1500000000; if (!itemsByColor[item2.color].empty()) { int costDifference = item2.cost - itemsByColor[item2.color].back().cost; itemsByColor[item2.color].back().costDifferenceToNext = costDifference; } itemsByColor[item2.color].push_back(item2); } for (auto & entryColor : itemsByColor) { if (!entryColor.empty()) { for (auto & entry : entryColor) { items2.push_back(entry); } } } std::sort(items2.begin(), items2.end(), [](const auto & left, const auto & right) { return left.costDifferenceToNext < right.costDifferenceToNext; }); // --------------- Searching for solution ------------------ std::vector<long long> individualSums(maxWeight, -1); individualSums[0] = 0; int count{1}; long long weightSum{0}; long long costSum{0}; // Get first entry to individualSums std::unordered_set<int> usedColors; for (auto entry : items2) { if (usedColors.find(entry.color) == usedColors.end()) { weightSum += entry.weight; weightSum %= maxWeight; costSum += entry.cost; usedColors.insert(entry.color); } } individualSums[weightSum] = costSum; // Get next entries based on using each color once for (int positionToSkip = 0; positionToSkip + 1 < items2.size(); positionToSkip++) { usedColors.clear(); weightSum = 0; costSum = 0; for (int i = 0; i < items2.size(); i++) { if (i == positionToSkip) { continue; } if (usedColors.find(items2[i].color) == usedColors.end()) { weightSum += items2[i].weight; weightSum %= maxWeight; costSum += items2[i].cost; usedColors.insert(items2[i].color); } } if (usedColors.size() == maxColor && (individualSums[weightSum] == -1 || individualSums[weightSum] > costSum)) { individualSums[weightSum] = costSum; } } for (int i = 2; i < 10; i++) { usedColors.clear(); weightSum = 0; costSum = 0; for (int colorId = 1; colorId < itemsByColor.size(); colorId++) { for (int j = 0; j < i; j++) { auto & item = itemsByColor[colorId][j % itemsByColor[colorId].size()]; weightSum += item.weight; weightSum %= maxWeight; costSum += item.cost; } } if (individualSums[weightSum] == -1 || individualSums[weightSum] > costSum) { individualSums[weightSum] = costSum; } } for (int colorA = 1; colorA <= maxColor; colorA++) { for (int colorB = 1; colorB <= maxColor; colorB++) { for (int colorC = 1; colorC <= maxColor; colorC++) { usedColors.clear(); weightSum = 0; costSum = 0; for (int colorId = 1; colorId < itemsByColor.size(); colorId++) { if (colorId == colorA) { auto & item = itemsByColor[colorId][0]; weightSum += item.weight; weightSum %= maxWeight; weightSum += item.weight; weightSum %= maxWeight; costSum += item.cost; costSum += item.cost; } else if (colorId == colorB) { auto & item = itemsByColor[colorId][0]; weightSum += item.weight; weightSum %= maxWeight; costSum += item.cost; auto & itemSecond = itemsByColor[colorId][(1 < itemsByColor[colorId].size() ? 1 : 0)]; weightSum += itemSecond.weight; weightSum %= maxWeight; costSum += itemSecond.cost; } else { auto & itemSecond = itemsByColor[colorId][(1 < itemsByColor[colorId].size() ? 1 : 0)]; weightSum += itemSecond.weight; weightSum %= maxWeight; costSum += itemSecond.cost; weightSum += itemSecond.weight; weightSum %= maxWeight; costSum += itemSecond.cost; } } if (individualSums[weightSum] == -1 || individualSums[weightSum] > costSum) { individualSums[weightSum] = costSum; } } } } for (auto cost : individualSums) { std::cout << cost << "\n"; } } private: int n; int maxColor; int maxWeight; std::vector<Item> items; }; int main() { int n, k, m; std::cin >> n >> k >> m; Solution solution(n, k, m); solution.run(); return 0; } |