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. linux文件管理之查找

    1 文件查找 1.1 which 查找可执行文件的路径which是通过 PATH环境变量到该路径内查找可执行文件,所以基本的功能是寻找可执行文件[root@www ~]# which [-a] com ...

  2. 20171023xlVBA递归统计WORD字数

    Dim dFilePath As Object, OneKey Sub main_proc() Dim Wb As Workbook, Sht As Worksheet, Rng As Range S ...

  3. 使用Vuex来处理Authentication token

    https://www.cnblogs.com/chentianwei/p/10156459.html 之前博客:建立了一个app.使用loacal storage 来管理tokens(通过clien ...

  4. WARNING: CPU: 0 PID: 1 at ./arch/x86/include/asm/fpu/internal.h:373

    ------------[ cut here ]------------WARNING: CPU: 0 PID: 1 at ./arch/x86/include/asm/fpu/internal.h: ...

  5. Tensor RT使用记录

    Tensor RT的介绍在此不做赘述. 自己在服务器上本打算装Tensor RT来着,不过过程很艰辛,最后发现服务器的cudnn版本偏低了,还需要升级cudnn的版本.故,在自己的电脑上了装了下Ten ...

  6. 2-sat学习笔记

    前后缀建图 例:要求n个变量满足至多有1个为true. 暴力:一个点的true向其它n-1个点的false连边,复杂度O(n^2). 正解:prei表示前i个点是否有真. prei的true向prei ...

  7. 漏洞复现——bash远程解析命令执行漏洞

    漏洞描述:Bash脚本在解析某些特殊字符串时出现逻辑错误导致可以执行后面的命令,在一些cgi脚本中,数据是通过环境变量来传递的,这样就会形成该漏洞 漏洞原理:bash通过以函数名作为环境变量名,以“( ...

  8. Windows自动登录设置 Windows免密登录

    设置方法如下:开始-运行-control userpasswords2:打开用户账号管理页面,将“要使用本机,用户必须输入用户名和密码”前面的勾去掉:点击 应用,确定之后.会提示用户输入需要自动登陆系 ...

  9. Hadoop介绍-3.HDFS介绍和YARN原理介绍

    一. HDFS介绍: Hadoop2介绍 HDFS概述 HDFS读写流程   1.  Hadoop2介绍 Hadoop是Apache软件基金会旗下的一个分布式系统基础架构.Hadoop2的框架最核心的 ...

  10. shiro权限管理的框架-入门

    shiro权限管理的框架 1.权限管理的概念 基本上涉及到用户参与的系统都要进行权限管理,权限管理属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制用户可以访问而且只能 ...