이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int limitWeight, numItem;
std::cin >> limitWeight >> numItem;
std::vector<std::array<int, 3>> items;
for (int index = 0; index < numItem; index++) {
int value, weight, amount;
std::cin >> value >> weight >> amount;
items.push_back({weight, value, amount});
}
sort(items.begin(), items.end(), [](const std::array<int, 3> &leftSide, const std::array<int, 3> &rightSide) {
if (leftSide[0] == rightSide[0]) {
return leftSide[1] > rightSide[1];
}
return leftSide[0] < rightSide[0];
});
std::vector<int> weightItem, valueItem;
std::vector<int> countItemWeight(limitWeight + 1);
for (const auto &[weight, value, amount] : items) {
int total = limitWeight / amount;
int index = 0;
while (countItemWeight[weight] < total && index < amount) {
countItemWeight[weight]++;
index++;
weightItem.emplace_back(weight);
valueItem.emplace_back(value);
}
}
std::vector<int> maxValue(limitWeight + 1);
for (int index = 0; index < (int)weightItem.size(); index++) {
for (int weight = limitWeight; weight > 0; weight--) {
if (weight >= weightItem[index]) {
maxValue[weight] = std::max(maxValue[weight], maxValue[weight - weightItem[index]] + valueItem[index]);
}
}
}
int result = 0;
for (int weight = 0; weight <= limitWeight; weight++) {
result = std::max(result, maxValue[weight]);
}
std::cout << result;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int main()':
knapsack.cpp:22:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
22 | for (const auto &[weight, value, amount] : items) {
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |