Younique @ 2019-01-05 19:49:45
先上代码
#include<bits/stdc++.h>
using namespace std;
int a[2000100];//定位坐标
int maxx[2000005];
int minn[2000005];
struct node{
int x;
int y;
}n[2000005];
int q,k;
void minique()
{
int i=1,head=1,tail=0;
for(i;i<k;i++)
{
while(head<=tail&&a[i]<=n[tail].x)
tail--;
n[++tail].x=a[i];
n[tail].y=i;
}
for(i;i<=q;i++)
{
while(head<=tail&&a[i]<=n[tail].x)
tail--;
n[++tail].x=a[i];
n[tail].y=i;
while(n[head].y<=i-k) head++;
maxx[head-k]=n[head].x;
cout<<maxx[head-k]<<" ";
}
}
void maxque()
{
memset(n,0,sizeof(n));
int i=1,head=1,tail=0;
for(i;i<k;i++)
{
while(head<=tail&&a[i]>=n[tail].x)
tail--;
n[++tail].x=a[i];
n[tail].y=i;
}
for(i;i<=q;i++)
{
while(head<=tail&&a[i]>=n[tail].x) tail--;
n[++tail].x=a[i];
n[tail].y=i;
while(n[head].y<=i-k) head++;
minn[head-k]=n[head].x;
cout<<minn[head-k]<<" ";
}
}
int main()
{
cin>>q>>k;
for(int i=1;i<=q;i++)
cin>>a[i];
minique();
cout<<endl;
maxque();
return 0;
}
题目里给的N是10^6 但为啥要开二倍数组才能过
不会Markdown的弱菜