An array of size n ≤ 10 6 is given to you. There is a sliding window of size kwhich 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 题意:给出n个数和区间长度m,然后求每个长度为m的区间的最大值和最小值
思路:因为题目所给的范围比较大,nlogn算法其实也可以,因为只有一组数据,但是我们把他作为滑动窗口的入门题来进行解析,滑动窗口是一个求一个区间的的值,区间长度固定的一个o(n)算法
下面首先我们熟悉下双端队列
头文件 #include<deque>
定义 deque<int> q;
头部插入 q.push_front()
头部删除 q.pop_front()
尾部插入 q.push_back()
尾部删除 q.pop_back()
取头值 q.front()
取尾值 q.back() 滑动窗口是一个维护一个队列,里面存的是最大值下表
最前的那个是当前区间最大值
给出一个例子
5 6 4 9 1
我们区间长度为2
开始5进入队列,然后因为6比5大,5就被踢出队列,6进来,因为队列最前面的就是区间里的最大值
然后4也到6得后面,因为如果6出去了,4就是当前得最大值了
后面9比4和6都大,就可以替换掉前面得数 主要思想:按顺序保存一个单调递减得序列,比他大得直接更新,小的后面区间用的到得一种思想,然后判断下标是否出区间就可以,每个数都最多进队列一次,出队列一次
然后这个提我们用两个双端队列 一个维护最大值,一个最小值即可
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<deque>
#include<vector>
#include<algorithm>
using namespace std;
int n,m;
int a[];
int b[];
int c[];
deque<int> qx,qn;
int cnt;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
while(!qx.empty()) qx.pop_front();
while(!qn.empty()) qn.pop_front();
for(int i=;i<n;i++)
scanf("%d",&a[i]);
for(int i=;i<n;i++)
{
while(!qx.empty()&&a[i]>=a[qx.back()])//判断是否新加得数比前面得大,大的话我们就要把最大值放最前,
qx.pop_back();
qx.push_back(i);
while(!qn.empty()&&a[i]<=a[qn.back()])
qn.pop_back();
qn.push_back(i);
if(i>=m-)
{
while(!qx.empty()&&qx.front()<=i-m) qx.pop_front();//我们把出了区间的数踢出队列
b[cnt]=a[qx.front()];
while(!qn.empty()&&qn.front()<=i-m) qn.pop_front();
c[cnt++]=a[qn.front()];
}
}
for(int i=;i<cnt;i++)
{
if(i==)
printf("%d",c[i]);
else printf(" %d",c[i]);
}
printf("\n");
for(int i=;i<cnt;i++)
{
if(i==)
printf("%d",b[i]);
else printf(" %d",b[i]);
}
printf("\n");
} }

POJ - 2823 Sliding Window (滑动窗口入门)的更多相关文章

  1. poj 2823 Sliding Window (单调队列入门)

    /***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...

  2. 洛谷P1886 滑动窗口(POJ.2823 Sliding Window)(区间最值)

    To 洛谷.1886 滑动窗口 To POJ.2823 Sliding Window 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每 ...

  3. POJ 2823 Sliding Window + 单调队列

    一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1)   从队首删除 (2)   从队尾删除 (3)   从队尾插入 (4)   ...

  4. POJ 2823 Sliding Window 题解

    POJ 2823 Sliding  Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...

  5. POJ 2823 Sliding Window & Luogu P1886 滑动窗口

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 66613   Accepted: 18914 ...

  6. POJ 2823 Sliding Window (滑动窗口的最值问题 )

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 41264   Accepted: 12229 ...

  7. 【POJ 2823】【Luogu P1886】Sliding Window 滑动窗口

    POJ 2823 Luogu P1886 [解题思路] 这是一个单调队列算法的经典题目,几乎学习单调队列的人都接触过这题. 利用单调队列算法求出每一个固定区间内的最(大/小)值. 以下以最大值为例: ...

  8. POJ 2823 Sliding Window(单调队列入门题)

      Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 67218   Accepted: 190 ...

  9. 题解报告:poj 2823 Sliding Window(单调队列)

    Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...

随机推荐

  1. 20170814xlVBA限定日期按客户分类汇总

    原始数据表: 汇总格式: Sub subtotalDic() Dim Wb As Workbook Dim Sht As Worksheet Dim oSht As Worksheet Dim mYe ...

  2. scrapy 爬虫框架(一)

    一 . scrapy 的安装 安装scrapy框架时,需要先安装依赖包. #Linux: pip3 install scrapy #Windows: a. pip3 install wheel b. ...

  3. 十分钟搞定pandas内容

    目录 十分钟搞定pandas 一.创建对象 二.查看数据 三.选择器 十二.导入和保存数据 参考:http://pandas.pydata.org/pandas-docs/stable/whatsne ...

  4. 『TensorFlow』SSD源码学习_其三:锚框生成

    Fork版本项目地址:SSD 上一节中我们定义了vgg_300的网络结构,实际使用中还需要匹配SSD另一关键组件:被选取特征层的搜索网格.在项目中,vgg_300网络和网格生成都被统一进一个class ...

  5. (待解决,效率低下)47. Permutations II C++回溯法

    思路是在相似题Permutations的基础上,将结果放到set中,利用set容器不会出现重复元素的特性,得到所需结果 但是利用代码中的/* */部分通过迭代器遍历set将set中的元素放在一个新的v ...

  6. python中RabbitMQ的使用(远程过程调用RPC)

    在RabbitMQ消息队列中,往往接收者.发送者不止是一个身份.例如接接收者收到消息并且需要返回给发送者. 此时接收者.发送者的身份不再固定! 我们来模拟该情形: 假设有客户端client,服务端se ...

  7. NOIP2003加分二叉树

    题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第ii个节点的分数为di,treedi,tree ...

  8. Django之信号和序列化

    前言 Django的信号要从一张抽象图和一个需求说起: 赛道:Django 赛车:http请求 基础设施:Django设置的信号 一.Django内置信号类型 1.既然赛道上有各种基础设置,那么Dja ...

  9. 【转】你必须知道的EF知识和经验

    注意:以下内容如果没有特别申明,默认使用的EF6.0版本,code first模式. 推荐MiniProfiler插件 工欲善其事,必先利其器. 我们使用EF和在很大程度提高了开发速度,不过随之带来的 ...

  10. volatile原理解析

    Java并发编程:volatile关键字解析 volatile 有序性.可见性 volatile可以保证一定程度上有序性,即volatile前面的代码先于后面的代码先执行. 但是前.后代码,各自里面的 ...