이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
void solve(){
int S,N;
cin>>S>>N;
map<long long, vector<vector<long long>>> m;
for(int i=1;i<=N;i++) {
long long v,w,k;
cin>>v>>w>>k;
m[w].push_back({v,k});
}
vector<vector<long long>> dp(2,vector<long long>(S+1));
int cur=1;
int last=0;
for(auto[w,vec]: m){
sort(vec.begin(),vec.end());
reverse(vec.begin(),vec.end());
// cout<<"weight: "<<w<<'\n';
// for(auto x:vec) cout<<x[0]<<' '<<x[1]<<'\n';
// cout<<'\n';
for(int j=S;j>=0;j--){
dp[cur][j] = dp[last][j];
int curitem=0;
int copy=0;
int tot_weight=0;
long long profit=0;
while(j>=(tot_weight+1)*w && curitem<vec.size()){
tot_weight++;
profit += vec[curitem][0];
dp[cur][j] = max(dp[cur][j], dp[last][j-tot_weight*w]+profit);
copy++;
if (copy==vec[curitem][1]){
curitem++;
copy=0;
}
}
}
swap(cur,last);
}
// for(auto x:dp){
// for(auto y:x)cout<<y<<' ';
// cout<<'\n';
// }
cout<<dp[m.size()%2][S]<<'\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
//cin>>t;
while(t--) solve();
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'void solve()':
knapsack.cpp:34:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | while(j>=(tot_weight+1)*w && curitem<vec.size()){
| ~~~~~~~^~~~~~~~~~~
# | 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... |