#include <deque> #include <cstdio> using namespace std;
struct num { int a, b; num(int x, int y) :a(x), b(y) { } };
void getMin(int* arr, int n, int k) { deque<num> q; for (int i = 0; i != n; i++) { while (!q.empty() && q.back().a >= arr[i]) q.pop_back(); num x(arr[i], i); q.push_back(x); if (i + 1 >= k) { while (i - q.front().b > k - 1) q.pop_front(); printf("%d ", q.front().a); } } }
void getMax(int* arr, int n, int k) { deque<num> q; for (int i = 0; i != n; i++) { while (!q.empty() && q.back().a <= arr[i]) q.pop_back(); num x(arr[i], i); q.push_back(x); if (i + 1 >= k) { while (i - q.front().b > k - 1) q.pop_front(); printf("%d ", q.front().a); } } }
int main() { int n, k; scanf_s("%d%d", &n, &k); int* arr = new int[n + 10]; for (int i = 0; i != n; i++) scanf_s("%d", &arr[i]); getMin(arr, n, k); printf("\n"); getMax(arr, n, k); printf("\n");
return 0; }
|