-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdequeue_stl.cpp
42 lines (38 loc) Β· 866 Bytes
/
dequeue_stl.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
#include <iostream>
#include <deque>
using namespace std;
void printKMax(int arr[], int n, int k) {
deque<int> deq(k); int i;
for(i=0 ; i < k ; i++) {
while(!deq.empty()&&arr[i]>=arr[deq.back()]) {
deq.pop_back();
}
deq.push_back(i);
}
for(i = k ; i < n ; i++) {
cout<<arr[deq.front()]<<" ";
while(!deq.empty() && deq.front()<=(i-k)) {
deq.pop_front();
}
while(!deq.empty()&&arr[i]>=arr[deq.back()]) {
deq.pop_back();
}
deq.push_back(i);
}
cout<<arr[deq.front()]<<endl;
}
int main(){
int t;
cin >> t;
while(t > 0) {
int n, k;
cin >> n >> k;
int i;
int arr[n];
for(i = 0 ; i < n ; i++)
cin >> arr[i];
printKMax(arr, n, k);
t--;
}
return 0;
}