제출 #948136

#제출 시각아이디문제언어결과실행 시간메모리
948136steveonalexKnapsack (NOI18_knapsack)C++11
100 / 100
52 ms3992 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define MASK(i) (1ULL << (i)) #define GETBIT(mask, i) (((mask) >> (i)) & 1) #define ALL(v) (v).begin(), (v).end() ll max(ll a, ll b){return (a > b) ? a : b;} ll min(ll a, ll b){return (a < b) ? a : b;} ll LASTBIT(ll mask){return (mask) & (-mask);} int pop_cnt(ll mask){return __builtin_popcountll(mask);} int ctz(ll mask){return __builtin_ctzll(mask);} int logOf(ll mask){return 63 - __builtin_clzll(mask);} mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);} template <class T1, class T2> bool maximize(T1 &a, T2 b){ if (a < b) {a = b; return true;} return false; } template <class T1, class T2> bool minimize(T1 &a, T2 b){ if (a > b) {a = b; return true;} return false; } template <class T> void printArr(T& container, string separator = " ", string finish = "\n", ostream &out = cout){ for(auto item: container) out << item << separator; out << finish; } template <class T> void remove_dup(vector<T> &a){ sort(ALL(a)); a.resize(unique(ALL(a)) - a.begin()); } const int S = 2001; vector<pair<int, int>> basket[S]; ll dp[S]; int main(void){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int s, n; cin >> s >> n; for(int i = 0; i<n; ++i){ int v, w, k; cin >> v >> w >> k; basket[w].push_back({v, min(k, s / w)}); } vector<pair<int, int>> sack; for(int i= 1; i<=s; ++i){ sort(ALL(basket[i])); for(int j = 0; j < s / i; ++j){ if (basket[i].empty()) break; sack.push_back({i, basket[i].back().first}); basket[i].back().second--; if (basket[i].back().second == 0) basket[i].pop_back(); } } for(auto item: sack){ for(int i = s; i>=item.first; --i) maximize(dp[i], dp[i - item.first] + item.second); } cout << dp[s] << "\n"; return 0; }
#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...