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. android-------- 多渠道打包(借助友盟移动统计分析)

    好久没有发博客了,原因是换工作了,今天端午假期,所以来发一篇博客, 多渠道打包,借助友盟移动统计分析,希望对各位有所帮助 多渠道打包的理解: 渠道包就是要在安装包中添加渠道信息,也就是channel, ...

  2. Vue音乐项目笔记(四)(搜索页面提取重写)

    1.如何通过betterScroll组件实现上拉刷新 https://blog.csdn.net/weixin_40814356/article/details/80478440 2.搜索页面跳转单曲 ...

  3. php多种方式获得文件扩展名

    /** * * 五种方法获取文件扩展名 **/ $file_name="aa.txt"; echo strrchr($file_name, '.'); echo '<hr/& ...

  4. Linux文件系统管理

    第九讲 文件系统管理   基础知识点:   硬盘的构成:主要由盘片.机械手臂.磁头与主轴马达所组成,实际的数据都是写在具有磁性物质上的盘片,通过主轴马达让盘片转动,机械手臂可伸展让磁头在盘片上读取数据 ...

  5. react中创建组件

    第1种 - 创建组件的方式 > 使用构造函数来创建组件,如果要接收外界传递的数据,需要在 构造函数的参数列表中使用`props`来接收:> 必须要向外return一个合法的JSX创建的虚拟 ...

  6. PGAdmin 4使用笔记

    1. import 默认username为postgres psql -U username -h localhost -d databasename -f "file address&qu ...

  7. 【LeetCode】字符串匹配

    给定目标串 haystack 和模式串 needle ,返回 needle 在 haystack 中第一次出现的位置下标,若 needle 不是 haystack 的子串则返回 -1. 1. Brut ...

  8. [Codeforces Round #340 (Div. 2)]

    [Codeforces Round #340 (Div. 2)] vp了一场cf..(打不了深夜的场啊!!) A.Elephant 水题,直接贪心,能用5步走5步. B.Chocolate 乘法原理计 ...

  9. 自定义putty主题

    PuTTY很早之前就没有更新了(0.62),因为都是开源的所以有人branch出来做了增强,如这个PuTTY tray,增加了超链等功能: https://puttytray.goeswhere.co ...

  10. MySQL变量变更小记

    MySQL会随版本的更新,在新版本中淘汰一些variable和引入一些新的variable.在配置variable后不起作用或安全扫描取不到variable值产生告警时,可能正是variable变更的 ...