Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 62930   Accepted: 17963
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
#include<cstdio>
const int N=1e6+88;
int a[N],q[N];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1; i<=n; ++i) scanf("%d",&a[i]);
    if(k==1)
    {
        for(int i=1; i<=n; ++i) printf("%d ",a[i]);
        puts("");
        for(int i=1; i<=n; ++i) printf("%d ",a[i]);
        puts("");
        return 0;
    }
    int h=1,t=1;
    q[1]=1;
    for(int i=2; i<=n; ++i)
    {
        if(i-q[h]==k) ++h;
        if(a[i]>a[q[t]]) q[++t]=i;
        else
        {
            while(t>=h&&a[i]<=a[q[t]]) q[t--]=i;
            ++t;
        }
        if(i>=k) printf("%d ",a[q[h]]);
    }
    puts("");
    h=1,t=1,q[1]=1;
    for(int i=2; i<=n; ++i)
    {
        if(i-q[h]==k) ++h;
        if(a[i]<a[q[t]]) q[++t]=i;
        else
        {
            while(t>=h&&a[i]>=a[q[t]]) q[t--]=i;
            ++t;
        }
        if(i>=k) printf("%d ",a[q[h]]);
    }
    puts("");
}
http://blog.csdn.net/acdreamers/article/details/20911981//学习的这个地方

poj2823单调队列认知的更多相关文章

  1. poj2823 单调队列初步

    什么是单调队列:头元素一直是队列当中的最大值,队列中的值按照递减顺序排列,可以从末尾插入一个元素,或从两段删除元素 1.插入元素,为了保证队列的单调性(这里假设为递减性),在插入元素v时要将对位的元素 ...

  2. 刷题向》POJ2823 单调队列裸题(<不会做,请自裁>系列)

    最近BZOJ炸了,而我的博客上又更新了一些基本知识,所以这里刷一些裸题,用以丰富知识性博客 POJ2823   滑动的窗口 这是一道经典的单调队题,我记得我刚学的时候就是用这道题作为单调队列的例题,算 ...

  3. POJ2823 单调队列

    POJ2823 http://poj.org/problem?id=2823 最基础的单调队列,说是数据结构,其实就是一种更新数组数据的方法. 之前还准备用deque,超时了,直接head,tail快 ...

  4. poj2823单调队列

    这个裸题,滑动窗口求最大最小值,单调队列来两边,一次单调递增q[s]就是最小值,一次单调递减q[s]就是最大值 cin会超时,解除同步也没用... #include<map> #inclu ...

  5. 单调队列(数列中长度不超过k的子序列和的最值)

    ★实验任务 小 F 很爱打怪,今天因为系统 bug,他提前得知了 n 只怪的出现顺序以及击 倒每只怪得到的成就值 ai.设第一只怪出现的时间为第 1 秒,这个游戏每过 1 秒 钟出现一只新怪且没被击倒 ...

  6. POJ2823 Sliding Window (单调队列)

    POJ2823 Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 38342   Accepte ...

  7. poj2823:单调队列入门题

    今天学习了一下单调队列这种数据结构,思想不是很难 参考资料:http://www.cnblogs.com/Jason-Damon/archive/2012/04/19/2457889.html 然后自 ...

  8. poj2823/hdu3415 - 数据结构 单调队列

    poj2823 题目链接 长度为N的数组,求宽度k的滑动窗口在数组上滑动时窗口内的最大值或最小值 如果用单调队列做,求最小值时,队列应该严格递增的.所以插入时,队尾大于等于插入值的元素都应被舍弃,因为 ...

  9. POJ2823 Sliding Window(单调队列)

    题目要输出一个序列各个长度k的连续子序列的最大值最小值. 多次RMQ的算法也是能过的,不过单调队列O(n). 这题,队列存元素值以及元素下标,队尾出队维护单调性然后入队,队首出队保持新元素下标与队首元 ...

随机推荐

  1. hdu_2570 迷障 贪心

    迷瘴 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  2. 标准SQL语句大全【持续更新】(navicat12版亲测有效)

    提示:用ctrl+F快速查找相关指令哦 -- 创建数据库 create database test_sql; -- 修改数据库名称(只有 sysadmin 和 dbcreator 固定服务器角色的成员 ...

  3. dispatch_async 的 block 中是否该使用_weak self

    问题分析 我看过很多文章关于在dispatch_async的block里面使用_weak self, 但是让我疑惑的是,以下代码是否需要必须使用_weak self, 因为我也看到了很多观点说,在有些 ...

  4. Visual Studio Code插件安装步骤

    1.进入扩展视图视图安装或卸载(快捷键Ctrl+shift+x) 转载于:https://www.cnblogs.com/SakalakaZ/p/7725159.html

  5. SaltStack数据系统之Grains、Pillar

    SaltStack数据系统之Grains.Pillar 1.什么是Grains? Grains是saltstack的组件,用于收集salt-minion在启动时候的信息,又称为静态信息.Grains是 ...

  6. 用Eclipse开发项目,你不能不知道的快捷键

    1. 编辑快捷键 编辑快捷键 介绍 psvm + Tab 生成main方法 sout + tab 生成输出语句 Ctrl+X / Ctrl + Y 删除一行 Ctrl+D 复制一行 Ctrl+/ 或 ...

  7. MySQL 子查询——查询最大值

    子查询指将一个查询语句嵌套在另一个查询语句中.子查询可以在 SELECT.UPDATE 和 DELETE 语句中使用,而且可以进行多层嵌套.在实际开发时,子查询经常出现在 WHERE 子句中.子查询在 ...

  8. vue后台管理系统介绍

    项目GitHub地址:https://github.com/Little-Orange7/cmms-vue 一.简介 很多公司稍微复杂一点的业务,基本上都需要后台管理系统,来对业务进行各个维度的统计. ...

  9. Java 经典面试题:聊一聊 JUC 下的 CopyOnWriteArrayList

    ArrayList 是我们常用的工具类之一,但是在多线程的情况下,ArrayList 作为共享变量时,并不是线程安全的.主要有以下两个原因: 1. ArrayList 自身的 elementData. ...

  10. js上传文件过大导致上传失败原因以及解决办法

    背景:项目需要用到上传视频功能,由于视频有知识产权,要求必须上传到自己的服务器上不允许用第三方视频网站接口上传,于是一开始开始用的是input type=file去上传,小的视频上传没有问题,上传将近 ...