求m区间内的最小值
题目描述
一个含有 n n n 项的数列,求出每一项前的 m m m 个数到它这个区间内的最小值。若前面的数不足 m m m 项则从第 1 1 1 个数开始,若前面没有数则输出 0 0 0。
输入格式
第一行两个整数,分别表示 n n n, m m m。
第二行, n n n 个正整数,为所给定的数列 a i a_i ai。
输出格式
n n n 行,每行一个整数,第 i i i 个数为序列中 a i a_i ai 之前 m m m 个数的最小值。
样例 #1
样例输入 #1
6 2
7 8 1 4 3 2
样例输出 #1
0
7
7
1
1
3
提示
对于 100 % 100\% 100% 的数据,保证 1 ≤ m ≤ n ≤ 2 × 1 0 6 1\le m\le n\le2\times10^6 1≤m≤n≤2×106, 1 ≤ a i ≤ 3 × 1 0 7 1\le a_i\le3\times10^7 1≤ai≤3×107。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<deque>
#define LL long long
using namespace std;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 1e9 + 10;
const int N = 1e6;
int n,k;
inline int rd()
{
int data = 0;
int f = 1;
char ch = getchar();
while(ch < '0'||ch > '9')
{
if(ch == '-')
f = -1;
ch = getchar();
}
while(ch >= '0'&&ch <= '9')
{
data = (data<<3) + (data<<1) + ch - '0';
ch = getchar();
}
return f * data;
}
struct node{
int val;
int pos;
}a[2 * N + 10];
deque<node> q;
int main(){
ios::sync_with_stdio(false);
cin >> n >> k;
for(int i = 1;i <= n;i ++)
{
cin >> a[i].val;
a[i].pos = i;
}
for(int i = 1;i < n;i ++)
{
if(i == 1)
{
q.push_back(a[i]);
cout << 0 << endl;
}
while(!q.empty()&&q.back().val >= a[i].val)
q.pop_back();
q.push_back(a[i]);
while(!q.empty()&&q.front().pos <= i - k)
q.pop_front();
printf("%d\n",q.front().val);
//cout << q.front().val << endl;
}
system("pause");
return 0;
}