程序在运行过程中出现错误,然后我用vs code调试了一下,看到什么因EXCEPTION已暂停。不是很懂啥情况。
代码:
#include<iostream>
#include<cstring>
using namespace std;
#define ll long long
const int maxn=1000010;
int a[1000010]={0};
int n,k;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void max1(){
int q[maxn],p[maxn],head,tail;
memset(q,0,sizeof(q));
head=1;tail=0;
for(int i=1;i<=n;i++){
while(a[i]>=q[tail]&&head<=tail){
tail--;
}
q[++tail]=a[i];
p[tail]=i;
while(p[head]<=i-k){
head++;
}
if(i>=k){
cout<<q[head]<<' ';
}
}
cout<<endl;
return ;
}
void min1(){
int q[maxn],p[maxn],head,tail;
memset(q,0,sizeof(q));
head=1;tail=0;
for(int i=1;i<=n;i++){
while(a[i]<=q[tail]&&head<=tail){
tail--;
}
q[++tail]=a[i];
p[tail]=i;
while(p[head]<=i-k){
head++;
}
if(i>=k){
cout<<q[head]<<' ';
}
}
cout<<endl;
return ;
}
int main(){
n=read();
k=read();
for(int i=1;i<=n;i++){
a[i]=read();
}
min1();
max1();
return 0;
}
输入:
8 3
1 3 -1 -3 5 3 6 7