-
Notifications
You must be signed in to change notification settings - Fork 8
/
maximum-of-all-subarrays-of-size-k.cpp
executable file
·58 lines (57 loc) · 1.4 KB
/
maximum-of-all-subarrays-of-size-k.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
//http://practice.geeksforgeeks.org/problems/maximum-of-all-subarrays-of-size-k/0
//maximum-of-all-subarrays-of-size-k
#include<bits/stdc++.h>
using namespace std;
int main(){
int t,n,k;
cin>>t;
while(t--){
int arr[105];
cin>>n>>k;
for(int i=0;i<n;i++){
cin>>arr[i];
}
//use deque
deque<int> q(k);
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()];
/*
multiset<int> s;
for(int i=0;i<k;i++){
s.insert(arr[i]);
}
multiset<int>:: reverse_iterator rt;
rt = s.rbegin();
cout<<(*rt)<<" ";
int j=0;
for(int i=k;i<n;i++){
multiset<int> :: iterator it;
it = s.find(arr[j]);
if(it != s.end()){
s.erase(it);
}
s.insert(arr[i]);
j++;
rt = s.rbegin();
cout<<(*rt)<<" ";
}*/
cout<<endl;
}
return 0;
}