ural 1126
2016-04-04 16:32
501 查看
1126. Magnetic StormsTime limit: 0.5 secondMemory 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. InputThe 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.Outputa sequence of readouts, each one in its line.Sample
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; }
相关文章推荐
- C#数据结构之顺序表(SeqList)实例详解
- Lua编程示例(三):稀疏表、双端队列、格式化输出、表和循环表的格式化输出
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- C#线程队列用法实例分析
- 算法系列15天速成 第九天 队列
- 数据结构之Treap详解
- C语言单链队列的表示与实现实例详解
- mysql 队列 实现并发读
- C#队列Queue用法实例分析
- C#多线程处理多个队列数据的方法
- C语言循环队列的表示与实现实例详解
- 用C语言举例讲解数据结构中的算法复杂度结与顺序表
- C++循环队列实现模型
- C#内置队列类Queue用法实例
- C#数据结构之堆栈(Stack)实例详解
- C#数据结构之双向链表(DbLinkList)实例详解