https://www.luogu.com.cn/problem/P1440
题目描述
一个含有 nn 项的数列,求出每一项前的 mm 个数到它这个区间内的最小值。若前面的数不足 mm 项则从第 11 个数开始,若前面没有数则输出 00。
输入格式
第一行两个整数,分别表示 nn,mm。
第二行,nn 个正整数,为所给定的数列 a_iai。
输出格式
nn 行,每行一个整数,第 ii 个数为序列中 a_iai 之前 mm 个数的最小值。
输入输出样例
输入 #1复制
6 2 7 8 1 4 3 2
输出 #1复制
0 7 7 1 1 3
说明/提示
对于 100\%100% 的数据,保证 1\le m\le n\le2\times10^61≤m≤n≤2×106,1\le a_i\le3\times10^71≤ai≤3×107。
模板题
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e6*2+10;
typedef int LL;
int n,m;
int q[maxn];
int a[maxn];
void min_queue()
{
LL h=1;LL t=0;
cout<<0<<endl;
for(LL i=1;i<=n-1;i++)
{
while(h<=t&&q[h]+m<=i) h++;
while(h<=t&&a[q[t]]>a[i]) t--;
q[++t]=i;
// if(i==1)
// cout<<0<<endl;
printf("%d\n",a[q[h]]);
}
}
int main(void)
{
//cin.tie(0);std::ios::sync_with_stdio(false);
scanf("%d%d",&n,&m);
for(LL i=1;i<=n;i++) scanf("%d",&a[i]);
min_queue();
return 0;
}