洛谷P1440 求m区间内的最小值

洛谷P1440

筱柒Littleseven
QQ:3534947775

题目链接

#include 
#include 
#include 
#include 
#define maxn 2000200
using namespace std;
int n,k,ans,a[maxn],q[maxn],minn[maxn];
int main()
{
  scanf("%d%d",&n,&k); //cin>>n>>k;
  for(int i=1;i<=n;i++) scanf("%d",&a[i]); //cin>>a[i];
  int head=1,tail=0;
  for(int i=1;i<=n;i++)
  {
  	while(head<=tail&&i-q[head]>=k) head++;
  	while(head<=tail&&a[q[tail]]>=a[i]) tail--;
  	q[++tail]=i; minn[i]=ans; ans=a[q[head]];
  }
  for(int i=1;i<=n;i++) printf("%d\n",minn[i]); //cout<
  return 0;
}

你可能感兴趣的:(单调队列题解)