-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSliding Window Mode.cpp
More file actions
56 lines (44 loc) · 920 Bytes
/
Copy pathSliding Window Mode.cpp
File metadata and controls
56 lines (44 loc) · 920 Bytes
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
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
set<pair<int,int>> freq_set;
map<int, int> freq_map;
void add(int v) {
int freq = freq_map[v];
freq_set.erase({-freq, v});
freq_set.insert({-(freq + 1), v});
freq_map[v]++;
}
void remove(int v) {
int freq = freq_map[v];
freq_set.erase({-freq, v});
if ( freq > 1 )
freq_set.insert({-(freq - 1), v});
freq_map[v]--;
}
int get() {
return freq_set.begin()->second;
}
void solve() {
int n, k;
cin >> n >> k;
int ara[n];
for(int i = 0; i < n; ++i) {
cin >> ara[i];
}
for(int i = 0; i < k; ++i) {
add(ara[i]);
}
cout << get() << " ";
for(int i = k; i < n; ++i) {
remove(ara[i-k]);
add(ara[i]);
cout << get() << " ";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}