이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define ll long long
#define int ll
#define pii pair<int,int>
#define tiii tuple<int,int,int>
using namespace std;
const ll INF = numeric_limits<ll>::max() / 2;
int32_t main() {
int s, n;
cin >> s >> n;
//vector<tiii> items(n);
vector<vector<tiii>> itemsPerWeight(s + 1);
for (int i = 0; i < n; i++) {
int v, w, k;
cin >> v >> w >> k;
//items[i] = {v, w, k};
if (w <= s) itemsPerWeight[w].push_back({ v, w, k });
}
vector<int> dp(s + 1, -INF);
dp[0] = 0;
// iterate through each block
for (int i = 1; i <= s; i++) {
// generate the block for this weight
priority_queue<pii> pq;
for (auto p : itemsPerWeight[i]) {
pq.push({ get<0>(p), get<2>(p) });
}
vector<int> values;
values.push_back(0);
for (int k = 1; k <= s / i; k++) {
if (pq.empty()) break;
auto p = pq.top();
pq.pop();
values.push_back(values[k - 1] + p.first);
if (p.second > 1) pq.push({ p.first,p.second-1 });
}
// do dp transitions
for (int k = s; k > 0; k--) {
for (int j = 0; j < values.size(); j++) {
if (k - (j * i) < 0) break;
dp[k] = max(dp[k], dp[k - (j * i)] + values[j]);
}
}
}
int ma = 0;
for (int i = 0; i <= s; i++) {
ma = max(ma, dp[i]);
}
cout << ma << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int32_t main()':
knapsack.cpp:45:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int j = 0; j < values.size(); j++) {
| ~~^~~~~~~~~~~~~~~
# | 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... |