Sliding Window

Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 66613   Accepted: 18914
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


题目描述

现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口。现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值。

例如:

The array is [1 3 -1 -3 5 3 6 7], and k = 3.

输入输出格式

输入格式:

输入一共有两行,第一行为n,k。

第二行为n个数(<INT_MAX).


输出格式:

输出共两行,第一行为每次窗口滑动的最小值

第二行为每次窗口滑动的最大值

输入输出样例

输入样例#1:

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

  

输出样例#1:

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

  

说明

50%的数据,n<=10^5

100%的数据,n<=10^6


有好多种玄学解法,但最快的是单调队列。

单调队列想学自己去找,这里直接上代码吧。

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#define MAXN 10000008
using namespace std;
int h,t;
int q[MAXN],p[MAXN],a[MAXN];
int n,k;
void findmin()
{
h=,t=;
for(int i=;i<=n;++i)
{
while(h<=t&&q[t]>=a[i])
t--;
q[++t]=a[i];
p[t]=i;
while(p[h]<=i-k)
h++;
if(i>=k)
cout<<q[h]<<' ';
}
}
void findmax()
{
h=,t=;
for(int i=;i<=n;++i)
{
while(h<=t&&q[t]<=a[i])
t--;
q[++t]=a[i];
p[t]=i;
while(p[h]<=i-k)
h++;
if(i>=k)
printf("%d ",q[h]);
}
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
findmin();
memset(p,,sizeof(p));
memset(q,,sizeof(q));
cout<<endl;
findmax();
}
作者:wlz
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

POJ 2823 Sliding Window & Luogu P1886 滑动窗口的更多相关文章

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

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

  2. POJ 2823 Sliding Window + 单调队列

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

  3. POJ 2823 Sliding Window 题解

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

  4. Luogu P1886 滑动窗口

    P1886 滑动窗口 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The a ...

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

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

  6. POJ - 2823 Sliding Window (滑动窗口入门)

    An array of size n ≤ 10 6 is given to you. There is a sliding window of size kwhich is moving from t ...

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

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

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

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

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

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

随机推荐

  1. Swift 中枚举

    Swift 中枚举高级用法及实践 字数11017 阅读479 评论0 喜欢20 title: "Swift 中枚举高级用法及实践"date: 2015-11-20tags: [AP ...

  2. LeetCode 206. Reverse Linked List (倒转链表)

    Reverse a singly linked list. 题目标签:Linked List 题目给了我们一个链表,要求我们倒转链表. 利用递归,新设一个newHead = null,每一轮 把下一个 ...

  3. CentOS-6.4-DVD系统中安装Oracle-11.2.0.4

    完整版见https://jadyer.github.io/2014/05/18/centos-install-oracle/ /** * CentOS-6.4-DVD系统中安装Oracle-11.2. ...

  4. C语言控制台窗体图形界面编程(总结)

    本系列文章是笔者通过学习<C语言控制台窗体界面编程(修正版)>而写(关于此文档的很多其它信息请看本系列文章第一篇),旨在让大家更加清晰简洁easy地学习C语言控制台窗体界面的编程. 通过本 ...

  5. system.web section group下的section

    private Configuration _configuration; private ConfigurationSectionGroupCollection sectionGroups; pri ...

  6. AngularJS2.0 quick start——其和typescript结合需要额外依赖

    AngularJS2 发布于2016年9月份,它是基于ES6来开发的. 运行条件! 由于目前各种环境(浏览器或 Node)暂不支持ES6的代码,所以需要一些shim和polyfill(IE需要)让ES ...

  7. LCA__st算法&&树上倍增

    st表 #include<cstdio> #include<algorithm> #include<cmath> using namespace std; ]; ] ...

  8. Scala 获取当前时间

    def NowDate(): String = { val now: Date = new Date() val dateFormat: SimpleDateFormat = new SimpleDa ...

  9. shell脚本-数组

    shell脚本-数组 数组 变量:存储单个元素的内存空间. 数组:存储多个元素的连续的内存空间,相当于多个变量的集合. 数组索引:编号从0开始,属于数值索引.索引可支持使用自定义的格式,而不仅是数值格 ...

  10. MAC地址 初识

    MAC地址 即物理地址/硬件地址 地址长度为48位,6字节. 格式为:00-23-5A-15-99-42 一个网卡对应一个MAC地址(比如笔记本,有线网卡有一个MAC地址,无线网卡也有一个MAC地址) ...