您的位置:首页 > 其它

Ural 1126. Magnetic Storms 单调队列

2013-07-29 21:43 239 查看


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

inputoutput
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: