// B5 - Desant #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <unordered_map> using namespace std; vector<long long> partialSums; long long rangeSum(int l, int r) { if (l == 0) { return partialSums[r]; } else { return partialSums[r] - partialSums[l - 1]; } } int main() { int n, k, q, l, r, a; vector<long long> values; vector<long long> candidates; vector<long long> fullResults; long long newCandidate; cin >> n; cin >> k; cin >> q; for (int i = 0; i < n; ++i) { cin >> a; values.push_back(a); partialSums.push_back(a); // fullResults.push_back(0); } // fullResults.push_back(0); for (int i = 1; i < n; ++i) { partialSums[i] += partialSums[i - 1]; } /* for (int i = n - 1; i >= 0; --i) { fullResults[i] = fullResults[i + 1]; if (i + k <= n) { newCandidate = fullResults[i + k] + rangeSum(i, i + k - 1); if (fullResults[i] < newCandidate) { fullResults[i] = newCandidate; } } } cout << "QWE "; for (int i = 0; i < n; ++i) { cout << fullResults[i] << " "; } cout << endl;*/ for (int z = 0; z < q; ++z) { cin >> l; cin >> r; --l; --r; candidates.clear(); for (int i = 0; i < n; ++i) { candidates.push_back(0); } candidates.push_back(0); for (int i = r - k + 1; i >= l; --i) { candidates[i] = candidates[i + 1]; newCandidate = candidates[i + k] + rangeSum(i, i + k - 1); if (candidates[i] < newCandidate) { candidates[i] = newCandidate; } } cout << candidates[l] << endl; } return 0; }
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | // B5 - Desant #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <unordered_map> using namespace std; vector<long long> partialSums; long long rangeSum(int l, int r) { if (l == 0) { return partialSums[r]; } else { return partialSums[r] - partialSums[l - 1]; } } int main() { int n, k, q, l, r, a; vector<long long> values; vector<long long> candidates; vector<long long> fullResults; long long newCandidate; cin >> n; cin >> k; cin >> q; for (int i = 0; i < n; ++i) { cin >> a; values.push_back(a); partialSums.push_back(a); // fullResults.push_back(0); } // fullResults.push_back(0); for (int i = 1; i < n; ++i) { partialSums[i] += partialSums[i - 1]; } /* for (int i = n - 1; i >= 0; --i) { fullResults[i] = fullResults[i + 1]; if (i + k <= n) { newCandidate = fullResults[i + k] + rangeSum(i, i + k - 1); if (fullResults[i] < newCandidate) { fullResults[i] = newCandidate; } } } cout << "QWE "; for (int i = 0; i < n; ++i) { cout << fullResults[i] << " "; } cout << endl;*/ for (int z = 0; z < q; ++z) { cin >> l; cin >> r; --l; --r; candidates.clear(); for (int i = 0; i < n; ++i) { candidates.push_back(0); } candidates.push_back(0); for (int i = r - k + 1; i >= l; --i) { candidates[i] = candidates[i + 1]; newCandidate = candidates[i + k] + rangeSum(i, i + k - 1); if (candidates[i] < newCandidate) { candidates[i] = newCandidate; } } cout << candidates[l] << endl; } return 0; } |