luogu: https://www.luogu.com.cn/problem/P1886

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, k, a[N];
void getMin(){
	vector <int> ans;
	deque <int> q;
	for (int i = 1; i <= n; i ++ ){
		while (q.size() && a[i] < a[q.back()]) q.pop_back();
		q.push_back(i);
		if (q.back() - q.front() + 1 > k) q.pop_front();
		if (i >= k) ans.push_back(q.front());
	}
	for (int i = 0; i < ans.size(); i ++ )
		cout << a[ans[i]] << " \n"[i == ans.size() - 1];
}
void getMax(){
	vector <int> ans;
	deque <int> q;
	for (int i = 1; i <= n; i ++ ){
		while (q.size() && a[i] > a[q.back()]) q.pop_back();
		q.push_back(i);
		if (q.back() - q.front() + 1 > k) q.pop_front();
		if (i >= k) ans.push_back(q.front());
	}
	for (int i = 0; i < ans.size(); i ++ )
		cout << a[ans[i]] << " \n"[i == ans.size() - 1];
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	cin >> n >> k;
	for (int i = 1; i <= n; i ++ )
		cin >> a[i];
	getMin();
	getMax();
	return 0;
}
posted on 2022-04-25 12:53  Hamine  阅读(20)  评论(0编辑  收藏  举报