-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathSlidingCost.cpp
48 lines (35 loc) Β· 1.05 KB
/
SlidingCost.cpp
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
#include <iostream>
#include <deque>
#include <vector>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> arr(n);
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
vector<int> cumulative_sum(n);
cumulative_sum[0] = arr[0];
for (int i = 1; i < n; ++i) {
cumulative_sum[i] = cumulative_sum[i - 1] + arr[i];
}
deque<int> window_indices;
vector<int> result;
for (int i = 0; i < n; ++i) {
while (!window_indices.empty() && window_indices.front() < i - k + 1) {
window_indices.pop_front();
}
while (!window_indices.empty() && arr[window_indices.back()] >= arr[i]) {
window_indices.pop_back();
}
window_indices.push_back(i);
int cost = (i - k >= 0) ? cumulative_sum[i] - cumulative_sum[i - k] : cumulative_sum[i];
result.push_back(cost);
}
for (int i = 0; i < n - k + 1; ++i) {
cout << result[i] << " ";
}
cout << endl;
return 0;
}