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. Redo日志

    undo日志有一个潜在的问题,即我们在将书屋改变的所有数据写到磁盘前不能提交该事务.有时,如果让数据库修改暂时只存在于主存中,我们可以节省磁盘IO;只要在崩溃发生时有日志可以恢复,这样做就是安全的. ...

  2. viewPager+Handler+Timer简单实现广告轮播效果

    基本思想是在Avtivity中放一个ViewPager,然后通过监听去实现联动效果,代码理由详细的解释,我就不说了. MainActivity.java package com.example.adm ...

  3. hdu 5233 Gunner II

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5233 简单题,stl水之... #include<algorithm> #include& ...

  4. iOS中MVC设计模式

    在组织大型项目的代码文件时,我们常用MVC的思想.MVC的概念讲起来非常简单,就和对象(object)一样.但是理解和应用起来却非常困难.今天我们就简单总结一下MVC设计理念. MVC(Model V ...

  5. Chr()和chrb()的含义(转)

    http://blog.csdn.net/cunxiyuan108/article/details/5989701 Chr(charcode) 必要的 charcode 参数是一个用来识别某字符的 L ...

  6. 【BZOJ 1033】 [ZJOI2008]杀蚂蚁antbuster

    Description 最近,佳佳迷上了一款好玩的小游戏:antbuster.游戏规则非常简单:在一张地图上,左上角是蚂蚁窝,右下角是蛋糕,蚂蚁会源源不断地从窝里爬出来,试图把蛋糕搬回蚂蚁窝.而你的任 ...

  7. 关于在 loadView 中改变状态栏的可视性

    这种问题不知道大家是否遇见过,在此用两句话(时间紧迫,还得加班)分享下今天犯的错误 我把状态栏的的可视性的改变写在了loadView 里面,然后就出现了调用了两次 loadView 和 viewDid ...

  8. responsive layout

    http://cssdeck.com/labs/7wsdvxdc http://getbootstrap.com/css/ http://getbootstrap.com/2.3.2/scaffold ...

  9. Think in java备忘录

    1..new在内部类中的使用 .new可以用使用外部类对象创建一个内部类,对象 DotNew.java package com.gxf.innerclass; public class DotNew ...

  10. Spark系列—01 Spark集群的安装

    一.概述 关于Spark是什么.为什么学习Spark等等,在这就不说了,直接看这个:http://spark.apache.org, 我就直接说一下Spark的一些优势: 1.快 与Hadoop的Ma ...