이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
#define endl "\n"
#define ii pair<int,int>
#define nd second
#define st first
using namespace std;
const int MAXS = 2e3 + 5;
const int MAXN = 1e5 + 5;
int n, dp[MAXS], s;
struct item{
int v, w, k;
} items[MAXN];
namespace SubTask{
void solve(){
for (int i = 1; i <= n; i++)
{
for (int w = s; w >= 0; w--)
{
for (int k = 1; k <= min(items[i].k, s / items[i].w); k++)
{
if (w - k * items[i].w >= 0)
dp[w] = max(dp[w], dp[w - k * items[i].w] + k * items[i].v);
}
}
}
cout << *max_element(dp, dp + s + 1) << endl;
}
}
namespace SubFull{
map<int, vector<ii> > bw;
void solve(){
for (int i = 1; i <= n; i++){
bw[items[i].w].push_back({items[i].v, items[i].k});
}
for (auto W : bw){
auto [item_w, item] = W;
sort(item.begin(), item.end(), greater<ii>());
int cnt_item = 0;
for (auto i : item) cnt_item += i.nd;
for (int w = s; w >= 0; w--){
int p = 0, cnt = 0, sum_value = 0;
for (int k = 1; k <= min(cnt_item, s / item_w); k++){
cnt++;
if (cnt > item[p].nd){
p++;
cnt = 1;
}
sum_value += item[p].st;
if (w - k * item_w >= 0){
dp[w] = max(dp[w], dp[w - k * item_w] + sum_value);
}
}
}
}
cout << *max_element(dp, dp + s + 1);
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> s >> n;
for (int i = 1; i <= n; i++){
cin >> items[i].v >> items[i].w >> items[i].k;
}
// if (n <= 100) SubTask::solve();
// else
SubFull::solve();
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'void SubFull::solve()':
knapsack.cpp:38:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
38 | auto [item_w, item] = W;
| ^
# | 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... |