题目描述
一个含有 n 项的数列,求出每一项前的 m 个数到它这个区间内的最小值。若前面的数不足 m 项则从第 1 个数开始,若前面没有数则输出 0。
输入格式
第一行两个整数,分别表示 n,m。(1 <= m, n <= 2*10^6)
第二行,n 个正整数,为所给定的数列 ai。(<= 10^5)
输出格式
n 行,每行一个整数ansi,第 i 个数为序列中 ai 之前 m 个数的最小值。
输入输出样例
输入 #1
6 2
7 8 1 4 3 2
输出 #1
0
7
7
1
1
3
#include<bits/stdc++.h>
using namespace std;
int N, K;
struct node{
int x, id;
}a[2000005];
deque<node> q;
int main()
{
cin >> N >> K;
for(int i=0; i<N; i++)
{
cin >> a[i].x; a[i].id = i;
}
cout << 0 << endl;
for(int i=0; i<N-1; i++)
{
while(q.empty() == 0 and q.front().id <= i-K) q.pop_front();
while(q.empty() == 0 and q.back().x >= a[i].x) q.pop_back();
q.push_back(a[i]);
cout << q.front().x << "\n";
}
return 0;
}