Ural 1126. Magnetic Storms 单调队列

1126. Magnetic Storms

Time limit: 0.5 second
Memory limit: 64 MB
The directory of our kindergarten decided to be attentive to the children's health and not to let them walk outdoors during magnetic storms. Special devices that measure and display magnetic intensity were ordered. If the readout exceeded some certain level the children were told to go indoors. They disliked it because they couldn't play their games up to the end. The nannies hated it because they had to dress and undress children many times.
After a while it became clear that one could try to forecast magnetic intensity because long periods of quietude alternated with short periods of plenty of sharp peaks (this is called a magnetic storm). Hence a new modification of the devices was ordered.
The new devices were to remember the situation within several last hours and to display the maximal intensity during the period. If the intensity was low within the last 6 hours the magnetic field was regarded to be quiet; the children were let outdoors and played all the prescript time. Otherwise new peaks were probable and the children spent their time indoors.
Your task is to write a program for a new version of the device. As a matter of fact you are to solve just the main problem of modification. All the rest is already done.
You are given a number  M which is length of a period (in seconds) within which peaks are to be stored and displayed. A sequence of measured magnetic intensity values is given to you as well. Each measurement is a number within the range from 0 to 100000.
Your are to output a sequence of values displayed by the device. The first number of the sequence is the maximal element of the first  M input numbers, the second number is the maximal element of the 2nd, …,  M+1-st input numbers and so on.
We hope that the new devices with your program won't go back on nannies and children will not walk during magnetic storms.

Input

The first line contains a number  M,  1 <  M ≤ 14000. Then values ( N integers) measured by the device follow each one in its line. There is a number −1 in the end.  M ≤  N ≤ 25000.

Output

a sequence of readouts, each one in its line.

Sample

input output
3
10
11
10
0
0
0
1
2
3
2
-1
11
11
10
0
1
2
3
3
Problem Author: Alexander Mironenko
Problem Source: VI Ural State University Collegiate Programming Contest (21.10.2001)
//单调队列,类似大根堆
#include <cstdio>
#include <cstdlib>
int n,m,a,l=1,r=1,Q[25001],pos[25001];
int main()
{
    scanf("%d",&m);
    for (int i=1; ; ++i)
    {
        scanf("%d",&a);
        if (a == -1)  break;
        while ((l <= r) && (i-pos[l] >= m))  ++l;
        while ((r >= l) && (Q[r] < a))  --r;
        Q[++r]=a;
        pos[r]=i;
        if (i >= m)  printf("%d\n",Q[l]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值