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. dbms_metadata.get_ddl的用法

    --GET_DDL: Return the metadata for a single object as DDL. -- This interface is meant for casual bro ...

  2. LeetCode 438. Find All Anagrams in a String (在字符串中找到所有的变位词)

    Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings ...

  3. shell中获取时间,作为文件夹格式

    近期写了一个小程序,须要用到时间.遍历作为文件夹的一部分 #!/bin/bash for i in {1..30} do date=`date +%Y/%m/%d -d " $i days ...

  4. cojs1101. [Vijos1369] 难解的问题==codevs 2188 最长上升子序列

    [题目描述] 在你的帮助下,蔚蓝来到了埃及.在金字塔里,蔚蓝看到了一个问题,传说,能回答出这个问题的人就能受到埃及法老的祝福,可是蔚蓝日夜奋战,还是想不出来,你能帮帮他么?(XXX: 胡扯,教主怎么可 ...

  5. bootstrap table load数据

    直接load数据: $("#button").click(function(){ var name=$("input[name='name']").val(); ...

  6. JSP-Runoob:JSP 表达式语言

    ylbtech-JSP-Runoob:JSP 表达式语言 1.返回顶部 1. JSP 表达式语言 JSP表达式语言(EL)使得访问存储在JavaBean中的数据变得非常简单.JSP EL既可以用来创建 ...

  7. php 静态属性和静态变量

  8. HDU 1847 博弈

    sg[0]=0; sg[i]=mex{sg[i-2^(j)]}  (i>=2^j) mex()为不在此集合的最小非负整数 #include <stdio.h> #include &l ...

  9. Maven密码加密

    第1步执行shell: mvn --encrypt-master-password  "SomeMadeUpMasterPassword" {nDpn1bE1vX4HABCDEFG ...

  10. mysql数据库存储的引擎和数据类型

    一.查看支持的存储引擎 SHOW ENGINES \G; 或者 SHOW VARIABLES LIKE 'have%'; 二.安装版mysql的默认引擎是InnoDB,免安装版默认引擎是MyISAM ...