이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
struct Item{
int value, weight, quantity;
};
vector<int>weights, values;
//vector<vector<long long> >table;
map<pair<int, int>, long long>table;
long long rec(int indx, int W){
if(W < 0)return -1e18;
if(indx == weights.size() || W == 0)return 0;
if(table.count({indx, W}))return table[{indx, W}];
return table[{indx, W}] = max(rec(indx + 1, W - weights[indx]) + values[indx], rec(indx + 1, W));
}
int main()
{
int S, n;
cin >> S >> n;
vector<Item>items;
for(int i = 0;i < n;++i){
int v, w, q;
cin >> v >> w >> q;
q = min(q, S / w);
Item cur;
cur.value = v;
cur.weight = w;
cur.quantity = q;
items.push_back(cur);
}
for(int i = 0;i < n;++i){
int v = items[i].value, w = items[i].weight, q = items[i].quantity;
for(int j = 0;j < q;++j){
weights.push_back(w);
values.push_back(v);
}
}
// table.assign(S + 1, vector<long long>(weights.size(), -1));
cout << rec(0, S);
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'long long int rec(int, int)':
knapsack.cpp:11:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | if(indx == weights.size() || W == 0)return 0;
| ~~~~~^~~~~~~~~~~~~~~~~
# | 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... |