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. Android开发之利用SQLite进行数据存储

    Android开发之利用SQLite进行数据存储 Android开发之利用SQLite进行数据存储 SQLite数据库简单介绍 Android中怎样使用SQLite 1 创建SQLiteOpenHel ...

  2. 【POJ3074】Sudoku DLX(Dancing Links)

    数独就要DLX,不然不乐意. 数独的DLX构造:9*9个点每一个点有9种选择,这构成了DLX的729行,每行.列.阵有限制,均为9行(/列/阵),然后每行(/列/阵)都有九种数的情况.于是就有了3*9 ...

  3. Weka算法Classifier-meta-AdaBoostM1源代码分析(一)

    多分类器组合算法简单的来讲经常使用的有voting,bagging和boosting,当中就效果来说Boosting略占优势,而AdaBoostM1算法又相当于Boosting算法的"经典款 ...

  4. X86架构下Linux启动过程分析

    1.X86架构下的从开机到Start_kernel启动的整体过程 这个过程简要概述为: 开机-->BIOS-->GRUB/LILO-->Linux Kernel 其执行的流程图和重要 ...

  5. 【BZOJ 2160】 拉拉队排练

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2160 [算法] 先简化题意 : 给定一个字符串,求最长的k个奇回文子串长度的乘积 先 ...

  6. MSP430 使用时 RST引脚一定拉高

    因为这次省去了RESET按键,RST没做任何处理,程序下载完之后插上电池无法运行,最终原因:RST引脚没有拉高.呼呼切记啊

  7. 【转载】基于AFNetWorking3.0的图片缓存分析

    原文出处: Yasin的简书 理论 不喜欢理论的可以直接跳到下面的Demo实践部分 缓存介绍 缓存按照保存位置可以分为两类:内存缓存.硬盘缓存(FMDB.CoreData…).我们常说的网络请求缓存包 ...

  8. python - list 列表推导式

    一.如有两个list,分别为: a = [1,2,3,4,5,6]b = ["a","b","c","d"," ...

  9. Tempter of the Bone------剪枝

    看了好多别人的  代码,最终还是 感觉 这种代码的风格适合我  下面附上代码 /* 首先 应该充满信心! 先写出来 自己的程序 然后慢慢改 , 如果是 答题思路错误的话 借鉴别人的 代码 再写 */ ...

  10. 在Swift中,如何像Objective-C定义可选接口?

    Objective-C中的protocol里存在@optional关键字,被这个关键字修饰的方法并非必须要被实现.我们可以通过接口定义一系列方法,然后由实现接口的类选择性地实现其中几个方法.在Coco ...