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. 刀哥多线程串行队列gcd-04-dispatch_queue_serial

    串行队列 特点 以先进先出的方式,顺序调度队列中的任务执行 无论队列中所指定的执行任务函数是同步还是异步,都会等待前一个任务执行完成后,再调度后面的任务 队列创建 dispatch_queue_t q ...

  2. DOS通讯录

    #include"stdio.h" #include"string.h" #include"stdlib.h" FILE *fp; #def ...

  3. uninstall 11.2.0.3.0 grid & database in linux 5.7

    OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - ...

  4. ios中怎么处理键盘挡住输入框

    //此前要遵循UITextFieldDelegate代理.并且设置该文本框为当前控制器的代理 //开始编辑的时候让整个view的高度网上移动 - (void)textFieldDidBeginEdit ...

  5. c++基础(二):成员he派生类

    struct Date{ int day, month, year; }; struct Book{ string name; Date date; void init(string n, int y ...

  6. IOS 其他 - 如何让 app 支持32位和64位

    让App支持32-bit和64-bit基本步骤 1.确保Xcode版本号>=5.0.1 2.更新project settings, minimum deployment target >= ...

  7. xcode 产生指定颜色的图片imageWithColor

    是在万能的stackOverflow上找到的答案,留下了, 原地址:http://stackoverflow.com/questions/6496441/creating-a-uiimage-from ...

  8. MVC 局部加载页面的实例

    我们在做MVC 进行某一块的局部刷新,有的使用AJAX 请求,有的使用局部页: 下面我给大家推荐一种使用局部页面实现的这种方式: 第一步: 嵌套视图页 <div id="showAud ...

  9. Java中的访问权限

    Java中有四种访问权限,从大到小依次是:public –> protected –> default(friendly) –> private. 简单说明下: public 作用域 ...

  10. Ubuntu 12.04 Desktop使用XAMPP

    Ubuntu 12.04 Desktop安装XAMPP Ubuntu 12.04 Desktop配置XAMPP Ubuntu 12.04 Desktop使用XAMPP 1/打开GUI界面的管理工具 终 ...