lemon2021 @ 2022-11-25 14:31:00
#include<iostream>
using namespace std;
int n;
int l,r;
int a[1000001];
int s1[1000001];
int s2[1000001];
int main()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++)
{
while (r&&a[i]<=a[s1[r-1]])
{
r--;
}
s1[r++]=i;
if(s1[l]<=i-k)
{
l++;
}
if(i>=k)
{
cout<<a[s1[l]]<<" ";
}
}
cout<<endl;
l=0;
r=0;
for(int i=1;i<=n;i++)
{
while (r&&a[i]>a[s2[r-1]])
{
r--;
}
s2[r++]=i;
if(s2[l]<=i-k)
{
l++;
}
if(i>=k)
{
cout<<a[s2[l]]<<" ";
}
}
return 0;
}
by LangQi99 @ 2022-11-25 14:37:15
https://www.luogu.com.cn/record/95379358
l<r
by LangQi99 @ 2022-11-25 14:37:57
#include<iostream>
using namespace std;
int n;
int l,r;
int a[1000001];
int s1[1000001];
int s2[1000001];
int main()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++)
{
while (l<r&&a[i]<=a[s1[r-1]])//here
{
r--;
}
s1[r++]=i;
if(s1[l]<=i-k)
{
l++;
}
if(i>=k)
{
cout<<a[s1[l]]<<" ";
}
}
cout<<endl;
l=0;
r=0;
for(int i=1;i<=n;i++)
{
while (l<r&&a[i]>a[s2[r-1]])//here
{
r--;
}
s2[r++]=i;
if(s2[l]<=i-k)
{
l++;
}
if(i>=k)
{
cout<<a[s2[l]]<<" ";
}
}
return 0;
}