-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem0154.h
73 lines (67 loc) · 1.49 KB
/
Problem0154.h
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
68
69
70
71
72
73
//
// Created by Fengwei Zhang on 2021/6/6.
//
#ifndef ACWINGSOLUTION_PROBLEM0154_H
#define ACWINGSOLUTION_PROBLEM0154_H
#include <iostream>
class Problem0154
{
private:
void print_max(const int &n, const int &k, const int *a, int *dq)
{
int hh = 0, tt = -1;
for (int i = 0; i < n; ++i)
{
while (hh <= tt && i - dq[hh] + 1 > k)
{
++hh;
}
while (hh <= tt && a[i] >= a[dq[tt]])
{
--tt;
}
dq[++tt] = i;
if (i + 1 >= k)
{
printf("%d ", a[dq[hh]]);
}
}
printf("\n");
}
void print_min(const int &n, const int &k, const int *a, int *dq)
{
int hh = 0, tt = -1;
for (int i = 0; i < n; ++i)
{
while (hh <= tt && i - dq[hh] + 1 > k)
{
++hh;
}
while (hh <= tt && a[i] <= a[dq[tt]])
{
--tt;
}
dq[++tt] = i;
if (i + 1 >= k)
{
printf("%d ", a[dq[hh]]);
}
}
printf("\n");
}
int main()
{
int n, k;
scanf("%d%d", &n, &k);
int a[n];
int dq[n];
for (int i = 0; i < n; ++i)
{
scanf("%d", &a[i]);
}
print_min(n, k, a, dq);
print_max(n, k, a, dq);
return 0;
}
};
#endif // ACWINGSOLUTION_PROBLEM0154_H