-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sliding Window Median.cpp
52 lines (40 loc) · 1.05 KB
/
Sliding Window Median.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
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
// template <typename Key, typename Mapped, typename Compare, typename Tag,
// typename Policy>
// class tree;
#define ll long long
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,
tree_order_statistics_node_update>
indexed_multiset;
int main() {
ll n, ans, k, ind = 0;
cin >> n >> k;
indexed_multiset ms;
vector<ll> v, nums;
for (ll i = 1; i <= n; i++) {
int x;
cin >> x;
nums.emplace_back(x);
if (i <= k) {
ms.insert({x, ind++});
}
}
vector<ll> vans(n - k + 1);
vans[0] = ms.find_by_order((k - 1) / 2)->first;
for (ll i = k; i < n; i++) {
ms.erase({nums[i - k], i - k});
ms.insert({nums[i], ind++});
// for (auto x : ms) {
// cout << x.first << " ";
// }
// cout << endl;
vans[i - k + 1] = ms.find_by_order((k - 1) / 2)->first;
}
// cout << " ---- " << endl;
for (auto x : vans)
cout << x << " ";
return 0;
}