您的位置:首页 > 理论基础 > 数据结构算法

ural 1126

2016-04-04 16:32 501 查看

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 childrenmany 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 numberM 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 firstM 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 numberM, 2 ≤M ≤ 14000. Then values (N integers) measured by the device follow each one in its line. Thereis 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)

Tags: data structures  (
hide tags for unsolved problems
)
Difficulty: 114    Printable version    Submit
solution    Discussion (56)
All submissions (13527)    All accepted submissions (5251)
   Solutions rating (3384)
题意:

  给一个数列,求每个长度为m的区间内最大值是多少?

思路:见代码

CODE:

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define pi acos(-1.0)
using namespace std;
typedef long long int LLI;
struct node {
int num;
int id;
};
deque <node> Que;//双端队列,保存的是到从i - m + 1到i中单调递减的元素
/*
我们可以从队列的末尾插入一个元素,可以从队列的两端删除元素。
1.首先看插入元素:为了保证队列的递减性,我们在插入元素v的时候,要将队尾的元素和v比较,如果队尾的元素不大于v,则删除队尾的元素,
然后继续将新的队尾的元素与v比较,直到队尾的元素大于v,这个时候我们才将v插入到队尾。
2.那么队首的元素什么时候删除呢?由于我们只需要保存i的前k-1个元素中的最大值,所以当队首的元素的索引或下标小于 i-k+1的时候,
就说明队首的元素对于求f(i)已经没有意义了,因为它已经不在窗里面了。所以当index[队首元素]<i-k+1时,将队首 元素删除。
3。每一次都返回队首元素(即最大值);
*/
int main() {
//    freopen("in.txt", "r", stdin);
//    freopen("25.out", "w", stdout);
int m,cnt = 0,n;
scanf("%d",&m);
while(scanf("%d",&n) && n != -1) {
cnt ++;
node op;
op.id = cnt;
op.num = n;
while(!Que.empty() && Que.front().id < cnt - m + 1)     Que.pop_front();//删除队首元素
while(!Que.empty() && Que.back().num <= n)              Que.pop_back();//删除队尾元素
Que.push_back(op);  //(1)对于每一个数字n,把n和其标号放到结构体内,然后放到双端队列中
if(cnt >= m)    printf("%d\n",Que.front());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息