이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<string.h>
#include<utility>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<functional>
#include<stack>
#include<limits.h>
#include<iomanip>
#include<unordered_map>
#include<numeric>
using namespace std;
#define MOD 1000000007
#define int long long
#define endl '\n'
bool scnd(pair<int, int> a,pair<int, int> b){
if(a!=b) return(a.first>b.first);
else return(a.second>b.second);
}
void solve(){
int s,n; cin>>s>>n;
vector<pair<int, int> > v[s+1];
for(int i=0; i<n; i++){
int a,b,c; cin>>a>>b>>c;
v[b].push_back(make_pair(a,c));
}
vector<pair<int, int> > f;
for(int i=1; i<=s; i++){
sort(v[i].begin(),v[i].end(),scnd);
for(int j=0,k=0; j<s/i; j++){
if(v[i].size()<=k) break;
if(!v[i][k].second) k++,j--;
else f.push_back(make_pair(i,v[i][k].first)),v[i][k].second--;
}
}
vector<int> dp(s+1);
for(int i=0; i<f.size(); i++){
for(int j=s; j>=f[i].first; j--){
dp[j]=max(dp[j],dp[j-f[i].first]+f[i].second);
}
}
cout<<dp[s];
}
signed main(){
// freopen("feast.in", "r", stdin);
// freopen("feast.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
cout<<endl;
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'void solve()':
knapsack.cpp:40:27: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
40 | if(v[i].size()<=k) break;
| ~~~~~~~~~~~^~~
knapsack.cpp:46:19: 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]
46 | for(int i=0; i<f.size(); i++){
| ~^~~~~~~~~
# | 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... |