P1440 求m区间内的最小值

求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 1mn2×106 1 ≤ a i ≤ 3 × 1 0 7 1\le a_i\le3\times10^7 1ai3×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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值