-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1077.cpp
84 lines (80 loc) · 2 KB
/
1077.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
int n, k, x[N];
ll sum_min = 0, sum_max = 0;
set<pair<int, int>> st_min, st_max;
void insert(int a, int id) {
if (!st_min.empty() && a >= st_min.rbegin()->first) {
sum_max += a;
st_max.emplace(a, id);
if (st_max.size() > st_min.size()) {
a = st_max.begin()->first;
sum_min += a;
sum_max -= a;
st_min.insert(*st_max.begin());
st_max.erase(st_max.begin());
}
} else {
sum_min += a;
st_min.emplace(a, id);
if (st_min.size() > st_max.size() + 1) {
a = st_min.rbegin()->first;
sum_max += a;
sum_min -= a;
st_max.insert(*st_min.rbegin());
st_min.erase(--st_min.end());
}
}
}
void remove(int a, int id) {
if (st_min.count({a, id})) {
sum_min -= a;
st_min.erase(st_min.find({a, id}));
if (st_max.size() > st_min.size()) {
a = st_max.begin()->first;
sum_min += a;
sum_max -= a;
st_min.insert(*st_max.begin());
st_max.erase(st_max.begin());
}
} else {
sum_max -= a;
st_max.erase(st_max.find({a, id}));
if (st_min.size() > st_max.size() + 1) {
a = st_min.rbegin()->first;
sum_max += a;
sum_min -= a;
st_max.insert(*st_min.rbegin());
st_min.erase(--st_min.end());
}
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> x[i];
}
for (int i = 1; i < k; ++i) {
insert(x[i], i);
}
for (int i = k; i <= n; ++i) {
if (i > k) {
remove(x[i - k], i - k);
}
insert(x[i], i);
ll cost = LLONG_MAX;
if (!st_min.empty()) {
ll a = st_min.rbegin()->first;
cost = min(cost, a * (ll)st_min.size() - sum_min + sum_max - a * (ll)st_max.size());
}
if (!st_max.empty()) {
ll a = st_max.begin()->first;
cost = min(cost, a * (ll)st_min.size() - sum_min + sum_max - a * (ll)st_max.size());
}
cout << cost << " \n"[i == n];
}
return 0;
}