-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARRAYSUB.cpp
45 lines (45 loc) · 914 Bytes
/
ARRAYSUB.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
#include<bits/stdc++.h>
using namespace std;
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int N;
cin >> N;
int *arr = new int[N];
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
int k;
cin >> k;
deque<pair<int, int>> q;
for (int i = 0; i < N; i++) {
if (q.empty()) {
q.push_back(make_pair(i, arr[i]));
if (i >= k - 1) {
cout << q.front().second << endl;
}
continue;
}
if (q.front().second <= arr[i]) {
while (!q.empty()) {
q.pop_front();
}
q.push_back(make_pair(i, arr[i]));
if (i >= k - 1) {
cout << q.front().second << endl;
}
} else {
while ((!q.empty()) && (i - q.front().first >= k)) {
q.pop_front();
}
while ((!q.empty()) && arr[i] >= q.back().second) {
q.pop_back();
}
q.push_back(make_pair(i, arr[i]));
if (i >= k - 1) {
cout << q.front().second << endl;
}
}
}
return 0;
}