# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1153084 | murpyl | Knapsack (NOI18_knapsack) | C++20 | 67 ms | 34376 KiB |
#include <bits/stdc++.h>
using namespace std;
#define int long long
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int,int>;
#define endl "\n"
#define mp make_pair
void setIO(string name = "") {
ios_base::sync_with_stdio(0); cin.tie(0);
if(sz(name)){
freopen((name+".in").c_str(), "r", stdin);
freopen((name+".out").c_str(), "w", stdout);
}
}
signed main(){
setIO();
int s, n;
cin>>s>>n;
map<int, vector<pi>> items;
for (int i = 0; i < n; i++){
int val, weight, quant;
cin>>val>>weight>>quant;
if (weight <= s && quant >= 0) items[weight].pb({val, quant});
//group shit by weight
//and u only care about s/w_i items
}
vector<vector<int>> dp(items.size()+1, vi(s+1, INT_MIN));
dp[0][0] = 0;
int i = 1;
for (auto &[weight, item]: items){
sort(all(item), greater<pi>());
for (int j = 0; j <= s; j++){
dp[i][j] = dp[i-1][j];
int profit = 0;
int cur = 0;
int used = 0;
int temp_used = 0;
// go through as many items until we run out of items or usable
// weight
while ((used+1)*weight<=j&&cur<item.size()){
used++;
temp_used++;
profit+=item[cur].first;
if (dp[i-1][j-used*weight] != INT_MIN){
dp[i][j] = max(dp[i][j], dp[i-1][j-used*weight]+profit);
}
if (temp_used == item[cur].second){
temp_used = 0;
cur++;
}
}
}
i++;
}
int ans = 0;
for (int i = 0; i <= s; i++){
ans = max(ans, dp[sz(items)][i]);
}
cout<<ans<<endl;
}
컴파일 시 표준 에러 (stderr) 메시지
# | 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... |