-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwoeditorials.cpp
70 lines (55 loc) · 1.6 KB
/
twoeditorials.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000;
int main() {
int n, m, k;
cin >> n >> m >> k;
int changes[maxn];
memset(changes, 0, sizeof(changes));
// get change array
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
changes[x]++;
changes[y + 1]--;
}
// get num of part. waiting for editorial at pos i
int active[maxn];
memset(active, 0, sizeof(active));
for (int i = 1; i <= n; i++) {
active[i] = active[i - 1] + changes[i];
}
// get sliding window sum for k length segment
int start[maxn];
memset(start, 0, sizeof(start));
int l = 1;
int r = k;
int curr_sum = 0;
curr_sum = accumulate(active + 1, active + k + 1, curr_sum);
start[1] = curr_sum;
while (r < n) {
l++, r++;
curr_sum -= active[l - 1];
curr_sum += active[r];
start[l] = curr_sum;
}
// get max possible sum left of i and right of i
int max_left[maxn];
int max_right[maxn];
memset(max_left, 0, sizeof(max_left));
memset(max_right, 0, sizeof(max_right));
for (int i = 1; i <= n; i++) {
max_left[i] = max(max_left[i - 1], start[i]);
}
for (int i = (n - k) + 1; i > 0; i--) {
max_right[i] = max(max_right[i + 1], start[i]);
}
// iterate over midline and keep track of best possible sum
int best = 0;
for (int i = 1; i <= (n - k + 1); i++) {
cout << max_left[i] << " " << max_right[i + k] << endl;
best = max(best, max_left[i] + max_right[i + k]);
}
cout << best << endl;
return 0;
}