-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFind max_element
More file actions
46 lines (36 loc) · 763 Bytes
/
Find max_element
File metadata and controls
46 lines (36 loc) · 763 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
#include <bits/stdc++.h>
using namespace std;
void printKMax(int arr[], int n, int k)
{
deque<int> q;
int i;
for (i = 0; i < k; ++i)
{
while ((!q.empty()) && arr[i] >= arr[q.back()])
q.pop_back();
q.push_back(i);
}
for (; i < n; ++i)
{
cout << arr[q.front()] << " ";
while ((!q.empty()) && q.front() <= i - k)
q.pop_front();
while ((!q.empty()) && arr[i] >= arr[q.back()])
q.pop_back();
q.push_back(i);
}
cout << arr[q.front()];
}
int main()
{
int n,k;
cin>>n;
int arr[n];
for(int i = 0 ; i < n ;i++)
{
cin>>arr[i];
}
cin>>k;
printKMax(arr, n, k);
return 0;
}