-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharraysub.cpp
54 lines (43 loc) · 1.04 KB
/
arraysub.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
#include<iostream>
#include<algorithm>
#include<vector>
#include<deque>
using namespace std ;
inline void print(int n){
cout << n << " ";
}
int main()
{
vector<int> v ;
int n, i, m,k,temp ;
// Input
cin >> n;
for(i = 0 ; i < n ; i++){
cin >> temp ;
v.push_back(temp);
}
cin >> k ;
// Processing
deque<int> window ;
// Add first k elements to the queue
for(i = 0 ; i < k ; i++){
// Remove the previous smaller elements from the queue
while( !window.empty() && v[i] >= v[window.back()]){
window.pop_back() ;
}
window.push_back(i);
}
print(v[window.front()]);
// Use Sliding window algo to find the next n-k maxs
for(;i < n; i++){
while(!window.empty() && window.front() <= i-k){
window.pop_front();
}
while( !window.empty() && v[i] >= v[window.back()]){
window.pop_back() ;
}
window.push_back(i) ;
print(v[window.front()]);
}
return 0 ;
}