제출 #1128404

#제출 시각아이디문제언어결과실행 시간메모리
1128404tuongllKnapsack (NOI18_knapsack)C++17
100 / 100
63 ms1888 KiB
#include <iostream> #include <cstring> #include <algorithm> #include <vector> #include <utility> #include <cmath> #include <ctime> #include <cassert> #include <set> #include <stack> #include <map> #include <queue> #include <random> #include <chrono> #include <bitset> #include <array> #include <sstream> #include <limits> using ll = long long; #define debug(x) cout << #x << " = " << x << '\n' #define separator "===============================================\n" #define all(a) a.begin(), a.end() #define SZ(a) (int)(a).size() using namespace std; const int mxn = 2e3 + 3; const ll mod = 1e9 + 7; const int inf32 = 2e9; const ll inf64 = 7e18; // #pragma GCC optimize("O3,unroll-loops") // #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") int S, n; vector<pair<int, int>> items[mxn], cand; int dp[mxn], new_dp[mxn]; void solve(){ cin >> S >> n; for (int i = 1, v, w, k; i <= n; ++i){ cin >> v >> w >> k; items[w].emplace_back(v, k); } for (int w = 1; w <= S; ++w){ sort(all(items[w]), greater<>()); int needed = (S + w - 1) / w; for (auto item : items[w]){ int v, k; tie(v, k) = item; int taken = min(k, needed); for (int i = 0; i < taken; ++i) cand.emplace_back(w, v); needed -= taken; } } for (auto item : cand){ int w, v; tie(w, v) = item; for (int i = 0; i <= S; ++i){ new_dp[i] = dp[i]; if (i >= w) new_dp[i] = max(new_dp[i], dp[i - w] + v); } for (int i = 0; i <= S; ++i) dp[i] = new_dp[i]; } cout << dp[S]; } int main(){ auto start = chrono::steady_clock::now(); ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin >> t; while(t--) solve(); chrono::duration<double> elapsed {chrono::steady_clock::now() - start}; cerr << "\n>> Runtime: " << elapsed.count() << "s\n"; }
#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...