제출 #899770

#제출 시각아이디문제언어결과실행 시간메모리
899770GhettoKnapsack (NOI18_knapsack)C++17
73 / 100
1065 ms3224 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]; deque<pli> trans[MAX_S]; 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]) { while (trans[c].size()) trans[c].pop_back(); trans[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[c % w[i]].size() && trans[c % w[i]].back().first <= val) trans[c % w[i]].pop_back(); trans[c % w[i]].push_back({val, c}); while (trans[c % w[i]].front().second < c - k[i] * w[i]) trans[c % w[i]].pop_front(); int new_c = trans[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...