이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<pair<ll,ll>> bycost[2005];
vector<pair<ll,ll>> item;
ll val[100005],cost[100005],freq[100005];
ll memo[2005][2005];
ll idx=0;
ll dp(ll x,ll y){
if(x==item.size()) return 0;
if(y<=0) return 0;
if(memo[x][y]!=-1) return memo[x][y];
//lewat
ll best=dp(x+1,y);
//ambil
if(y>=item[x].second){
best=max(best,dp(x+1,y-item[x].second)+item[x].first);
}
return memo[x][y]=best;
}
int main(){
int t,n;cin>>t>>n;
for(ll i=1;i<=n;i++){
cin>>val[i]>>cost[i]>>freq[i];
bycost[cost[i]].push_back({val[i],freq[i]});
}
bool masih=true;
for(ll j=1;j<=2000;j++){
int tot=0;
if(bycost[j].size()>0){
sort(bycost[j].begin(),bycost[j].end(),greater<pair<ll,ll>>());
for(auto nx:bycost[j]){
for(int i=1;i<=nx.second;i++){
tot+=j;
item.push_back({nx.first,j});
idx++;
if(tot>=2000){
break;
masih=false;
}
}
if(!masih) break;
}
}
if(!masih) break;
}
memset(memo,-1,sizeof memo);
cout<<dp(0,t);
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'long long int dp(long long int, long long int)':
knapsack.cpp:12:7: 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]
12 | if(x==item.size()) return 0;
| ~^~~~~~~~~~~~~
# | 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... |