-
Notifications
You must be signed in to change notification settings - Fork 0
/
deque.cpp
67 lines (49 loc) · 1.12 KB
/
deque.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
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
void printKMax(int arr[], int n, int k)
{
if (n < k) {
return;
}
int steps = n - k + 1;
std::deque<int> mydeque {};
for (int i = 0; i < k; i++) {
mydeque.push_back(arr[i]);
}
int max = *std::max_element(mydeque.begin(), mydeque.end());
std::cout << max << " ";
for (int i = k; i < n; i++) {
int front = mydeque.front();
mydeque.pop_front();
mydeque.push_back(arr[i]);
if (front == max) {
max = *std::max_element(mydeque.begin(), mydeque.end());
} else {
max = std::max(max, arr[i]);
}
std::cout << max << " ";
}
std::cout << std::endl;
}
int main()
{
/* int arr[] {3, 4, 5, 8, 1, 4, 10};
printKMax(arr, 7, 4);
return 0;
*/
int t;
cin >> t;
while (t--)
{
int n, k;
cin >> n >> k;
int arr[n] {};
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
printKMax(arr, n, k);
}
return 0;
}