Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 35941   Accepted: 10636
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

 
RMQ 
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int MAX_N = 1e6 + ;
int N,K;
int mi[MAX_N],a[MAX_N],ma[MAX_N];
int k; void RMQ() {
for(int j = ; j <= k; ++j) {
for(int i = ; i + ( << j) - <= N; ++i) {
mi[i] = min(mi[i],mi[i + ( << (j - ))]);
ma[i] = max(ma[i],ma[i + ( << (j - ))]);
}
}
} int main()
{
//freopen("sw.in","r",stdin);
while(~scanf("%d%d",&N,&K)) {
for(int i = ; i <= N; ++i) {
scanf("%d",&a[i]);
ma[i] = mi[i] = a[i];
} k = ;
while(( << (k + )) < K) ++k;
RMQ();
for(int i = ; i <= N - K + ; ++i) {
printf("%d%c",min(mi[i],mi[i + K - ( << k)]),i == N - K + ? '\n' : ' ');
} for(int i = ; i <= N - K + ; ++i) {
printf("%d%c",max(ma[i],ma[i + K - ( << k)]),i == N - K + ? '\n' : ' ');
} } return ;
}

单调队列

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int MAX_N = 1e6 + ;
int N,K;
int a[MAX_N];
int mi[ * MAX_N],ma[ * MAX_N];
int ans[MAX_N]; void solve() {
int s = ,e = ;
for(int i = ; i <= N; ++i) {
while(s < e && a[i] < a[ mi[e - ] ]) --e;
mi[e++] = i;
if(i - K + >= ) {
ans[i - K + ] = a[ mi[s] ];
}
if(mi[s] == i - K + ) ++s;
} for(int i = ; i + K - <= N; ++i) {
printf("%d%c",ans[i],i == N - K + ? '\n' : ' ');
} s = ,e = ;
for(int i = ; i <= N; ++i) {
while(s < e && a[i] > a[ ma[e - ] ]) --e;
ma[e++] = i;
if(i - K + >= ) {
ans[i - K + ] = a [ ma[s] ];
}
if(ma[s] == i - K + ) ++s;
} for(int i = ; i + K - <= N; ++i) {
printf("%d%c",ans[i],i == N - K + ? '\n' : ' ');
} } int main() {
while(~scanf("%d%d",&N,&K)) {
for(int i = ; i <= N; ++i) {
scanf("%d",&a[i]);
} solve();
} return ; }

POJ 2823的更多相关文章

  1. POJ 2823 Sliding Window + 单调队列

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

  2. POJ 2823 Sliding Window 题解

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

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

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

  4. 【POJ 2823】Sliding Window(单调队列/堆)

    BUPT2017 wintertraining(16) #5 D POJ - 2823 题意 给定n,k,求滑窗[i,i+k-1]在(1<=i<=n)的最大值最小值. 题解 单调队列或堆. ...

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

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

  6. caioj 1172 poj 2823 单调队列过渡题

    给定一个n个数的数列,从左至右输出每个长度为m的数列段内的最大数. 输入:第一行两个整数n和m( 1<= n <= 20 0000,m<=n).下来给出n个整数. 输出:一行一个整数 ...

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

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

  8. Sliding Window POJ - 2823 单调队列模板题

    Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...

  9. POJ 2823 滑动窗口 单调队列模板

    我们从最简单的问题开始: 给定一个长度为N的整数数列a(i),i=0,1,...,N-1和窗长度k. 要求: f(i) = max{a(i-k+1),a(i-k+2),..., a(i)},i = 0 ...

  10. POJ 2823 Sliding Window 【单调队列】

    题目链接:http://poj.org/problem?id=2823 题目大意:给出一组数,一个固定大小的窗体在这个数组上滑动,要求出每次滑动该窗体内的最大值和最小值. 这就是典型的单调队列,单调队 ...

随机推荐

  1. PHP闭包(Closure)初探

    不知不觉发现PHP已经出到了5.5版本,而自己一直在用PHP5.2,让我看起来像深山出来的小伙子一样,又土又落后.在我习惯在javascript中使用闭包之后,忽然间对PHP的闭包打起了兴趣. 于是乎 ...

  2. 条款38:通过聚合设计has-a或者is-implemented-in-terms-of

    聚合:类型之间的一种关系,就是一种类型内含有另一种类型的变量. has-a: class Address { }; class PhoneNumber { }; class Person { publ ...

  3. java数据结构和算法------选择排序

    package iYou.neugle.sort; public class Select_sort { public static void SelectSort(double[] array) { ...

  4. python学习笔记1

    python3.3使用urllib2报错 no module named urllib2,原因是python3中将urllib2换成了request. 所以要使用import urllib.reque ...

  5. [shell基础]——paste命令

    测试文本内容如下: # cat name1.txt name1 alvin1 name2 alvin2 name3 alvin3 name4 alvin4 # cat name2.txt name1 ...

  6. linux查看端口是否已开启和查看文件数

    查看端口是否开启 lsof -i:80 查看文件数 ls | wc -w

  7. nodejs笔记四--创建一个最简单的 express 应用

    express 是 Node.js 应用最广泛的 web 框架,利用 express 可以实现很多的web应用:首先需要需要得到一个express. 新建一个文件夹叫lesson1,进去里面安装 ex ...

  8. dmucs与distcc

    之前配置distcc没有考虑负载均衡这一项,现在考虑使用dmucs实现distcc的负载均衡 官方手册 http://dmucs.sourceforge.net/ 使用官方手册编译会报错,等解决问题后 ...

  9. Ztree的初步使用--checkbox--指定目录下搜索子节点

    这里记录一下zTree的check的使用 首先 <%@ Page Language="C#" AutoEventWireup="true" CodeBeh ...

  10. js—对象

    一.创建对象 var car = new Object(); var car = {}; 二.属性和方法 var car = { color : "red", run : func ...