이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
struct item
{
int v, w, k;
};
int n, s;
item a[100001];
vector<item> b;
int dp[15520][2001];
vector<map<int, int, greater<int> > > lis(2001);
bool cmp(item &a, item &b)
{
if(a.w == b.w) return a.v > b.v;
return a.w < b.w;
}
void print()
{
for(int i = 1; i < lis.size(); i++){
cout<<"Weight "<<i<<": "<<endl;
for(auto x : lis[i]){
cout<<x.first<<" "<<x.second<<endl;
}
cout<<endl;
}
}
int main()
{
//freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
cin>>s>>n;
for(int i = 0; i < n; i++){
cin>>a[i].v>>a[i].w>>a[i].k;
}
sort(a, a + n, cmp);
//for(int i = 0; i < n; i++) cout<<a[i].v<<" "<<a[i].w<<" "<<a[i].k<<endl;
int cnt = 0, wei = a[0].w;
for(int i = 0; i < n; i++){
//if(a[i].w == wei){
if(s / a[i].w >= cnt + a[i].k){
lis[a[i].w][a[i].v] = a[i].k;
cnt += a[i].k;
}
else{
lis[a[i].w][a[i].v] = s / a[i].w - cnt;
cnt = 0;
}
//}
if(a[i + 1].w != a[i].w) cnt = 0;
}
//print();
b.push_back({0, 0, 0});
for(int i = 1; i < lis.size(); i++){
for(auto x : lis[i]){
int t = x.second;
while(t--){
item tmp = {x.first, i, 1};
b.push_back(tmp);
}
}
}
//for(int i = 0; i < b.size(); i++) cout<<b[i].w<<" "<<b[i].v<<endl;
for(int i = 1; i < b.size(); i++){
int v = b[i].v, w = b[i].w;
for(int j = 0; j <= s; j++){
dp[i][j] = dp[i - 1][j];
if(j - w >= 0) dp[i][j] = max(dp[i][j], dp[i - 1][j - w] + v);
}
}
cout<<dp[b.size() - 1][s];
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'void print()':
knapsack.cpp:23:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::map<int, int, std::greater<int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i = 1; i < lis.size(); i++){
| ~~^~~~~~~~~~~~
knapsack.cpp: In function 'int main()':
knapsack.cpp:64:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::map<int, int, std::greater<int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int i = 1; i < lis.size(); i++){
| ~~^~~~~~~~~~~~
knapsack.cpp:76:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<item>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for(int i = 1; i < b.size(); i++){
| ~~^~~~~~~~~~
knapsack.cpp:46:18: warning: unused variable 'wei' [-Wunused-variable]
46 | int cnt = 0, wei = a[0].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... |