跳到正文

个人记录

ghnujbdfkl

hhsyh约 1 分钟
原文
文章信息与版本

文章信息

发布
2026年2月28日
收录
2026年2月28日
更新
2026年8月24日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,k,a[N];
deque<int>q;
int main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++){
		while(!q.empty()&&a[q.back()]>a[i])q.pop_back();
		q.push_back(i);
		if(i>=k){
			while(!q.empty()&&q.front()<i-k+1)q.pop_front();
			cout<<a[q.front()]<<" ";
		}
	}
	cout<<"\n";
	while(!q.empty())q.pop_front();
		for(int i=1;i<=n;i++){
		while(!q.empty()&&a[q.back()]<a[i])q.pop_back();
		q.push_back(i);
		if(i>=k){
			while(!q.empty()&&q.front()<i-k+1)q.pop_front();
			cout<<a[q.front()]<<" ";
		}
	}
	return 0;
}
原文