이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
//By Tejas India, https://www.linkedin.com/in/1tejasjain/
// void call(){
// int s, n; cin>>s>>n;
// vector<int> maxValueByWeight(s+1, 0);
// maxValueByWeight[0] = 0;
// for(int item=1; item<=n; item++){
// int value, weight, quantity; cin>>value>>weight>>quantity;
// // cout<<"Item: "<<item<<endl;
// for(int i=1; i<=quantity; i++)
// for(int ks = s; ks>=0; ks--){
// if(ks-weight>=0)
// maxValueByWeight[ks] = max(maxValueByWeight[ks], maxValueByWeight[ks-weight]+value);
// // cout<<ks<<": "<<maxValueByWeight[ks]<<" ";
// }
// // cout<<endl;
// }
// cout<<maxValueByWeight[s]<<endl;
// }
void call(){
int s, n; cin>>s>>n;
vector<int> maxValueByWeight(s+1, 0);
maxValueByWeight[0] = 0;
map<int, vector<pair<int, int>>> byWeight;
for(int item=1; item<=n; item++){
int value, weight, quantity; cin>>value>>weight>>quantity;
byWeight[weight].push_back({value, quantity});
}
for(auto [weight, vec]: byWeight){
sort(vec.begin(), vec.end(), greater<pair<int, int>>());
for(int ks = s; ks>=0; ks--){
int copies = 0;
int type_at = 0;
int curr_used = 0;
long long profit = 0;
// go through as many items until we run out of items or usable
// weight
while ((copies + 1) * weight <= ks && type_at < vec.size()) {
copies++;
profit += vec[type_at].first;
if (maxValueByWeight[ks - copies * weight] != INT32_MIN) {
maxValueByWeight[ks] = max(maxValueByWeight[ks], maxValueByWeight[ks - copies * weight] + profit);
}
curr_used++;
if (curr_used == vec[type_at].second) {
curr_used = 0;
type_at++;
}
}
// for(auto [value, quantity]: vec){
// for(int i=1; i<=quantity; i++){
// if(ks-weight>=0)
// maxValueByWeight[ks] = max(maxValueByWeight[ks], maxValueByWeight[ks-weight]+value);
// // cout<<ks<<": "<<maxValueByWeight[ks]<<" ";
// }
// // cout<<endl;
// }
}
}
cout<<maxValueByWeight[s]<<endl;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t=1;
//cin>>t;
while(t--){
call();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'void call()':
knapsack.cpp:43:50: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | while ((copies + 1) * weight <= ks && type_at < 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... |