Submission #899775

#TimeUsernameProblemLanguageResultExecution timeMemory
899775GhettoKnapsack (NOI18_knapsack)C++17
73 / 100
1083 ms147892 KiB
#include <bits/stdc++.h> using namespace std; using lint = long long; using pli = pair<lint, int>; const int MAX_N = 1e5 + 5, MAX_S = 2000 + 5; int s, n; lint v[MAX_N]; int w[MAX_N], k[MAX_N]; lint dp[2][MAX_S]; unordered_map<int, deque<pli>> trans[MAX_N]; int main() { // freopen("knapsack.in", "r", stdin); cin >> s >> n; for (int i = 1; i <= n; i++) { cin >> v[i] >> w[i] >> k[i]; k[i] = min(k[i], s); } for (int i = n; i >= 1; i--) { int parity = i % 2, opp_parity = (i + 1) % 2; for (int c = 0; c <= s; c++) { if (c < w[i]) { trans[i][c].push_back({dp[opp_parity][c], c}); dp[parity][c] = dp[opp_parity][c]; } else { lint val = dp[opp_parity][c] - v[i] * (c / w[i]); while (trans[i][c % w[i]].size() && trans[i][c % w[i]].back().first <= val) trans[i][c % w[i]].pop_back(); trans[i][c % w[i]].push_back({val, c}); while (trans[i][c % w[i]].front().second < c - k[i] * w[i]) trans[i][c % w[i]].pop_front(); int new_c = trans[i][c % w[i]].front().second; dp[parity][c] = dp[opp_parity][new_c] + v[i] * ((c - new_c) / w[i]); } // cout << i << " " << c << ": " << dp[parity][c] << endl; } } cout << dp[1][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...