Submission #563122

#TimeUsernameProblemLanguageResultExecution timeMemory
563122HmmOkKewlKnapsack (NOI18_knapsack)C++14
100 / 100
252 ms247812 KiB
#include <bits/stdc++.h> #if ONLINE_JUDGE #define DEBUG(...) #else #define DEBUG(kek) cout << "DEBUG: [ " << #kek << " = " << kek << " ]\n" #endif typedef long long ll; typedef unsigned long long ull; typedef long double ld; #define ff first #define ss second #define vt(...) vector<__VA_ARGS__> #define sz(...) (int)(__VA_ARGS__).size() #define pii pair<int, int> #define pll pair<ll, ll> #define pb push_back #define mp make_pair #define all(x) x.begin(), x.end() constexpr int M = 1000000007; constexpr int MONKE = 0; using namespace std; template <typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1, T2>& a) { os << "{" << a.first << ", " << a.second << "}"; return os; } template <typename T> ostream& operator<<(ostream& os, vector<T> container) { os << "\n[ "; for (auto x : container) os << x << " "; os << "]\n"; return os; } template <typename T1, typename T2> bool sort_by_second_greater(const pair<T1, T2>& a, const pair<T1, T2>& b) { return (a.ss > b.ss); } int main() { // unsync with C stream :) ios_base::sync_with_stdio(MONKE); cin.tie(0); // todo int S, n; cin >> S >> n; vector<tuple<int, int, int>> items(n); for (int i = 0; i < n; ++i) { int v, w, k; cin >> v >> w >> k; items[i] = {v, w, k}; } vt(vt(pii)) groups(S + 1); for (int i = 0; i < n; ++i) { int idx = get<1>(items[i]); groups[idx].pb({get<0>(items[i]), get<2>(items[i])}); } vector<int> value, weight; for (int i = 1; i <= S; ++i) sort(all(groups[i]), greater<pii>()); int validItems = 0; for (int i = 1; i <= S; ++i) { int mxItems = S / i; int taken = 0; int itmIdx = 0; while (taken < mxItems && itmIdx < sz(groups[i])) { int takes = min(mxItems - taken, groups[i][itmIdx].ss); for (int j = 0; j < takes; ++j) { value.pb(groups[i][itmIdx].ff); weight.pb(i); } taken += takes; itmIdx++; } validItems += taken; } vt(vt(long long)) maxValue(S + 1, vt(long long)(validItems, 0)); // cerr << value; // cerr << weight; for (int i = weight[0]; i <= S; ++i) maxValue[i][0] = value[0]; for (int i = 1; i <= S; ++i) { for (int j = 1; j < validItems; ++j) { maxValue[i][j] = max(maxValue[i][j - 1], (i >= weight[j] ? maxValue[i - weight[j]][j - 1] + value[j] : 0)); } } cout << maxValue[S][validItems - 1]; return MONKE; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...