-
Notifications
You must be signed in to change notification settings - Fork 222
/
deque-stl.cpp
47 lines (37 loc) · 871 Bytes
/
deque-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
43
44
45
46
47
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
void printKMax(int arr[], int n, int k) {
std::deque<int> dq(k);
int i;
for (i = 0; i < k; ++i) {
while ( (!dq.empty()) && arr[i] >= arr[dq.back()])
dq.pop_back();
dq.push_back(i);
}
for ( ; i < n; ++i) {
cout << arr[dq.front()] << " ";
while ( (!dq.empty()) && dq.front() <= i - k)
dq.pop_front();
while ( (!dq.empty()) && arr[i] >= arr[dq.back()])
dq.pop_back();
dq.push_back(i);
}
cout << arr[dq.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;
}