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. hdu 2034 人见人爱A-B

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2034 人见人爱A-B Description 参加过上个月月赛的同学一定还记得其中的一个最简单的题目, ...

  2. Erlang generic standard behaviours -- gen

    在分析 gen_server (或者是gen_fsm )之前,首先应该弄明白,gen 这个module . -module(gen). -compile({inline,[get_node/1]}). ...

  3. ElasticSearch版本升级备忘录(1.5.2至2.3.1)

    discovery机制默认为单播,需配置discovery.zen.ping.unicast.hosts:,如果各节点使用默认端口,则只配IP即可(["172.17.4.47", ...

  4. js 获取字符串中最后一个斜杠后面的内容

    var str = "/asdasf/asfaewf/agaegr/trer/rhh"; var index = str .lastIndexOf("\/"); ...

  5. 微信支付.NET版开发总结(JS API),好多坑,适当精简。

    前2天,做一个手机网页的微信支付的项目,费了好些周折,记录一下.接下来,按照开发步骤,细数一下,我遇到的那些坑. [坑1]官方邮件中下载的demo只有PHP版本,其他版本没有给链接.可能让人误以为只有 ...

  6. IIS 8.5配置.net网站[花了半个多小时]

    1.默认安装了IIS部分功能.参考http://www.cnblogs.com/xuanhun/p/4201645.html 2.运用程序连接池, 使用集成 模式 3.运用程序连接池,设置启用32位应 ...

  7. ABAP字符串按长度拆分

    REPORT ytest_012 MESSAGE-ID oo. ) TYPE c. ) TYPE c. ) TYPE c. DATA: l_pos TYPE i. DATA: BEGIN OF ls_ ...

  8. String、StringBuilder、StringBuffer

    String                                                                                        String ...

  9. ueditor:原谅我这一生不羁放纵爱独特

    客户指明道姓需要使用百度编辑器,好吧,虽然自从李彦宏把一个好好的千千静听搞得节操尽碎之后,我就对百度的东西毫无好感,但是客户是上帝嘛,不就一个文本编辑器嘛,弄之,始料未及的是,就是这样一个简单的文本编 ...

  10. MySQL 主从数据库设置

    1.复制的介绍 MySQL 支持单向.异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护文件的一个索引 以跟踪日志循环.这些日志可 ...