#include <bits/stdc++.h> using namespace std; int64_t brute_dp(vector<int64_t>& team_sum, int k, int l, int r) { vector<int64_t> dp(k); int i = l - 1; for (; i < r - k + 1; i++) { int64_t o1 = dp[(i - k + dp.size()) % dp.size()] + team_sum[i]; int64_t o2 = dp[(i - 1 + dp.size()) % dp.size()]; dp[i % dp.size()] = max(o1, o2); } return dp[(i - 1) % dp.size()]; } int main() { // freopen("D:/szkola/des/des0.in", "r", stdin); ios_base::sync_with_stdio(0); cin.tie(0); int n, k, q; cin >> n >> k >> q; vector<int> as(n); for (auto& a : as) { cin >> a; } vector<int64_t> team_sum(n - k + 1); // team that starts at given pos for (int i = 0; i < n; i++) { if (i < k) { team_sum[0] += as[i]; } else { team_sum[i - k + 1] = team_sum[i - k]; team_sum[i - k + 1] += as[i]; team_sum[i - k + 1] -= as[i - k]; } } while (q--) { int l, r; cin >> l >> r; cout << brute_dp(team_sum, k, l, r) << "\n"; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include <bits/stdc++.h> using namespace std; int64_t brute_dp(vector<int64_t>& team_sum, int k, int l, int r) { vector<int64_t> dp(k); int i = l - 1; for (; i < r - k + 1; i++) { int64_t o1 = dp[(i - k + dp.size()) % dp.size()] + team_sum[i]; int64_t o2 = dp[(i - 1 + dp.size()) % dp.size()]; dp[i % dp.size()] = max(o1, o2); } return dp[(i - 1) % dp.size()]; } int main() { // freopen("D:/szkola/des/des0.in", "r", stdin); ios_base::sync_with_stdio(0); cin.tie(0); int n, k, q; cin >> n >> k >> q; vector<int> as(n); for (auto& a : as) { cin >> a; } vector<int64_t> team_sum(n - k + 1); // team that starts at given pos for (int i = 0; i < n; i++) { if (i < k) { team_sum[0] += as[i]; } else { team_sum[i - k + 1] = team_sum[i - k]; team_sum[i - k + 1] += as[i]; team_sum[i - k + 1] -= as[i - k]; } } while (q--) { int l, r; cin >> l >> r; cout << brute_dp(team_sum, k, l, r) << "\n"; } } |