이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*Segment tree really makes clean code look ugly :P*/
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN=2e5+5;
const int INF=1e18;
const int MOD=1e9+7;
const int MAXL=13;
const int MAXW=2e3+5;
int dp[2][MAXW];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//cout<<setprecision(6)<<fixed;
//freopen("feast.in","r",stdin);
//freopen("feast.out","w",stdout);
int s,n;
cin>>s>>n;
vector<vector<int>>sack;
for(int i=0;i<n;i++) {
int v,w,k;
cin>>v>>w>>k;
k=min(k,s/w);
int j=MAXL-1;
for(j=MAXL-1;((1LL<<j)&k)==0;j--);
int x=k-(1LL<<j)+1;
sack.push_back({v*x,w*x});
j--;
while(j>=0) {
sack.push_back({v*(1LL<<j),w*(1LL<<j)});
j--;
}
}
int curr=1;
for(int i=0;i<sack.size();i++) {
int nxt=(curr^1);
for(int j=0;j<=s;j++) {
dp[curr][j]=0;
if(sack[i][1]<=j) {
dp[curr][j]=max(dp[curr][j],dp[nxt][j-sack[i][1]]+sack[i][0]);
}
dp[curr][j]=max(dp[curr][j],dp[nxt][j]);
}
curr=nxt;
}
curr^=1;
int ans=0;
for(int j=0;j<=s;j++) {
ans=max(ans,dp[curr][j]);
}
cout<<ans;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int main()':
knapsack.cpp:36:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for(int i=0;i<sack.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... |