-
Notifications
You must be signed in to change notification settings - Fork 1
/
18-IPL-2021-Match-Day-2.cpp
54 lines (45 loc) · 1.08 KB
/
18-IPL-2021-Match-Day-2.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
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> max_of_subarrays(vector<int> arr, int n, int k)
{
vector<int> ans;
priority_queue<pair<int, int>> pq;
int i = 0, j = 0;
while(j < k)
{
pq.push({arr[j], j});
j++;
}
ans.push_back(pq.top().first);
if(pq.top().second == 0)
pq.pop();
i = 1;
while(j < n)
{
pq.push({arr[j], j});
while(!pq.empty() && pq.top().second < i)
pq.pop();
ans.push_back(pq.top().first);
i++;
j++;
}
return ans;
}
};
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> arr(n);
for (int i = 0; i < n; i++) cin >> arr[i];
Solution ob;
vector<int> res = ob.max_of_subarrays(arr, n, k);
for (int i = 0; i < res.size(); i++) cout << res[i] << " ";
cout << endl;
}
return 0;
}