Sliding Window
链接:http://poj.org/problem?id=2823
Time Limit: 12000MS   Memory Limit: 65536K
     
Case Time Limit: 5000MS

Description

An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example: 
The array is [1 3 -1 -3 5 3 6 7], and k is 3.

Window position Minimum value Maximum value
[1  3  -1] -3  5  3  6  7  -1 3
 1 [3  -1  -3] 5  3  6  7  -3 3
 1  3 [-1  -3  5] 3  6  7  -3 5
 1  3  -1 [-3  5  3] 6  7  -3 5
 1  3  -1  -3 [5  3  6] 7  3 6
 1  3  -1  -3  5 [3  6  7] 3 7

Your task is to determine the maximum and minimum values in the sliding window at each position.

Input

The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line. 

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values. 

Sample Input

8 3
1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7

Source

POJ Monthly--2006.04.28, Ikki
题意:给定一个数列,从左至右输出每个长度为m的数列段内的最小数和最大数。
数列长度:N<=106,m<=N
#include<iostream>
#include<cstdio>
using namespace std;const int N = 1000005;int n,k,m,mn[N],mx[N],n1,n2;struct node{int id, data;
};node dj[N],dz[N];//dz 递增队列 dj 递减队列 int main()
{cin >> n >> k;int headj = 1,tail1 = 0,headz = 1,tail2 = 0;for(int i = 1; i <= n; i++){scanf("%d",&m);while(m <= dj[tail1].data && tail1 >= headj) tail1 --;//保持单调性 while(m >= dz[tail2].data && tail2 >= headz) tail2 --;dj[++tail1].data = m;dj[tail1].id = i;dz[++tail2].data = m;dz[tail2].id = i;if(i >= k){//开始记录第n段中的最值 if(dj[headj].id <= i-k) mn[++n1] = dj[++headj].data;//过期了 else mn[++n1] = dj[headj].data;if(dz[headz].id <= i-k) mx[++n2] = dz[++headz].data;else mx[++n2] = dz[headz].data;}}for(int i = 1; i <= n1; i++)printf("%d ",mn[i]);cout<<endl;for(int i = 1; i <= n1; i++)printf("%d ",mx[i]);return 0;
}

第一次手打队列,以后还是用双向吧

转载于:https://www.cnblogs.com/EdSheeran/p/8406551.html

POJ 2823 Sliding Window相关推荐

  1. POJ 2823 Sliding Window(单调队列)

    http://poj.org/problem?id=2823 题意: 给出数组和滑动窗口的大小,每次输出滑动窗口中的最大值和最小值. 思路: 这题可以算是单调队列的模板题了,分别维护单调递增和单调递减 ...

  2. POJ 2823 Sliding Window (单调队列)

    单调队列 加了读入挂比不加更慢.... 而且这份代码要交c++ 有大神G++跑了700ms..... orzorzorz #include<iostream> #include<cs ...

  3. 【POJ】2823 Sliding Window

    单调队列. 1 /* 2823 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> ...

  4. 【POJ - 2823】 Sliding Window(单调队列 用双端队列实现或模拟队列)

    题干: An array of size n ≤ 10 6 is given to you. There is a sliding window of size k which is moving f ...

  5. POJ2823 Sliding Window【单调队列】【线段树】【ST表】

    Sliding Window POJ - 2823 题意: 给出一个长度为N的序列,通过一个窗口,可以看到序列中连续的K个元素,窗口从最左边出发,每次移动一个单位,对于每次移动,输出当前窗口中的最大值 ...

  6. Sliding Window滑动窗口(单调队列)

    Sliding Window滑动窗口 POJ - 2823 目录 Sliding Window滑动窗口 POJ - 2823 题意描述 解题思路 AC代码 An array of size n ≤ 1 ...

  7. LeetCode 滑动窗口(Sliding Window)类问题总结

    导语 滑动窗口类问题是面试当中的高频题,问题本身其实并不复杂,但是实现起来细节思考非常的多,想着想着可能因为变量变化,指针移动等等问题,导致程序反复删来改去,有思路,但是程序写不出是这类问题最大的障碍 ...

  8. python实现滑动窗口平均_数据流滑动窗口平均值 · sliding window average from data stream...

    [抄题]: 给出一串整数流和窗口大小,计算滑动窗口中所有整数的平均值. MovingAverage m = new MovingAverage(3); m.next(1) = 1 // 返回 1.00 ...

  9. LeetCode 239. Sliding Window Maximum

    原题链接在这里:https://leetcode.com/problems/sliding-window-maximum/ 题目: Given an array nums, there is a sl ...

最新文章

  1. dw读取access中的图片_怎样从Access数据库中读取图片?解决办法
  2. PostgreSQL JDBC SQLWarning
  3. 选对论文,效率提升50% | 本周值得读
  4. 华为云阳云计算外包给哪家公司的_长春作为东北中心,华为四大件已经配齐,绝了!...
  5. Observer(订阅与发布)
  6. 文献学习(part47)--A novel consensus learning approach to incomplete multi-view clustering
  7. Absolute C++ Chapter 3 Self-Test Exercise(3)
  8. CSS那些不大不小的事
  9. pytorch搭建TextRNN与使用案例
  10. python对比图片
  11. 【Arcpy】Python in ArcGIS
  12. 电机学(2) - 变压器
  13. 富士康计算机主板官网,富士康主板官网?富士康主板刷bios工具?foxconn主板官网?富士康主板怎么样...
  14. 无锡关于计算机青少年的比赛,无锡市青少年机器人竞赛开赛 1200多名选手投身“编程王国”...
  15. html中header怎么设置,怎么在html中设置header
  16. 俄文输入法_如何在手机上添加俄语输入法
  17. 计算机音乐咱们结婚吧音乐谱,齐晨咱们结婚吧简谱_咱们结婚吧歌词
  18. Hard Swish激活函数
  19. Adaptive Graph Convolutional Neural Networks
  20. 无法创建目录d oracle,Qt无法创建目录(Qt could not create directory)

热门文章

  1. 《Java开发手册》解读:大整数传输为何禁用Long类型?
  2. 免费下载 | 超全算法题精解,一本能“在线”编程的面试宝典
  3. 登录form php一个页面跳转页面,Extjs4中表单登录功能、登录成功跳转页面的代码...
  4. Linux复习资料(一)、VM虚拟机安装教程
  5. spark环境搭建(idea版本)
  6. Oracle里面的用户user无法登录 LOCKED(TIMED)
  7. 远程桌面连接数超过最大限制解决方法
  8. 服务器后端 项目代码常用目录图
  9. Python基础(一)简介与安装
  10. Linux常用命令和常见问题解决------第一章