George1123 @ 2019-07-13 10:19:46
#include <bits/stdc++.h>
using namespace std;
int n,m,a[1000010];
queue<int> Min,Max;
int ans[2][1000010];
int np,xp;
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
// cout<<"!JOU!"<<endl;
for(int i=1;i<=m;i++){
if(Min.empty()) Min.push(a[i]);
else{
int p=Min.front();
while(p>=a[i]&&!Min.empty()){
Min.pop();
np++;
if(Min.empty()) break;
p=Min.front();
} Min.push(a[i]);
} if(Max.empty()) Max.push(a[i]);
else{
int q=Max.front();
while(q<=a[i]&&!Max.empty()){
Max.pop();
xp++;
if(Max.empty()) break;
q=Max.front();
} Max.push(a[i]);
} ans[0][0]=Min.front();
ans[1][0]=Max.front();
} for(int i=m+1;i<=n;i++){
if(np<i-m) Min.pop();
if(xp<i-m) Max.pop();
if(Min.empty()) Min.push(a[i]);
else{
int p=Min.front();
while(p>=a[i]&&!Min.empty()){
Min.pop();
np++;
if(Min.empty()) break;
p=Min.front();
} Min.push(a[i]);
} if(Max.empty()) Max.push(a[i]);
else{
int q=Max.front();
while(q<=a[i]&&!Max.empty()){
Max.pop();
xp++;
if(Max.empty()) break;
q=Max.front();
} Max.push(a[i]);
}
ans[0][i-m]=Min.front();
ans[1][i-m]=Max.front();
} for(int j=0;j<=1;j++)
for(int i=0;i<=n-m;i++)
cout<<ans[j][i]<<" \n"[i==n-m];
return 0;
}
样例中“凹”的情况怎么处理?