#include <cstdio>
#include <deque>
using namespace std;
long long int n,k,a[1000030];
deque<long long int> t;
int main(){
scanf("%lld%lld",&n,&k);
for (long long int i = 1;i <= n;i++) scanf("%lld",&a[i]);
for (long long int i = 1;i <= n;i++){
if (t.front() == i - k) t.pop_front();
while (!t.empty()){
if (a[i] < a[t.back()]) t.pop_back();
else break;
}
t.push_back(i);
if (i >= k) printf("%lld ",a[t.front()]);
}
t.clear();
printf("\n");
for (long long int i = 1;i <= n;i++){
if (t.front() == i - k) t.pop_front();
while (!t.empty()){
if (a[i] > a[t.back()]) t.pop_back();
else break;
}
t.push_back(i);
if (i >= k) printf("%lld ",a[t.front()]);
}
return 0;
}
测试点 3 报错(RE):Received signal 11: Segmentation fault with invalid memory reference.。
尝试了 n=k=1、n>k=1 等特殊情况都没发现问题。