Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 49919   Accepted: 14391
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 题意:模拟滑动窗口,每k个数,输出最大值最小值
单调队列:
从这里学来的http://m.blog.csdn.net/blog/lx417147512/24916441
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string.h>
using namespace std;
const int N = + ;
int num[N],Max[N],Min[N],Increase[N],Decrease[N];
int pre1,pre2,lst1,lst2,len_max,len_min,n,k;
void in_Max(int i)
{
while(pre1 <= lst1 && num[i] > num[ Increase[lst1] ])
lst1--;
Increase[++lst1] = i;
if(i >= k)
{
if(Increase[pre1] <= i - k)
pre1++;
Max[len_max++] = num[ Increase[pre1] ];
}
}
void in_Min(int i)
{
while(pre2 <= lst2 && num[i] < num[ Decrease[lst2] ])
lst2--;
Decrease[++lst2] = i;
if(i >= k)
{
if(Decrease[pre2] <= i - k)
pre2++;
Min[len_min++] = num[ Decrease[pre2] ];
}
}
int main()
{
while(scanf("%d%d", &n,&k) != EOF)
{
pre1 = pre2 = len_max = len_min = ;
lst1 = lst2 = -; //注意这个要设成-1
for(int i = ; i <= n; i++)
{
scanf("%d",&num[i]);
in_Max(i);
in_Min(i);
}
printf("%d",Min[]);
for(int i = ; i < len_min; i++)
printf(" %d",Min[i]);
printf("\n");
printf("%d",Max[]);
for(int i = ; i < len_max; i++)
printf(" %d",Max[i]);
printf("\n");
}
return ;
}

POJ2823Sliding Window的更多相关文章

  1. poj2823Sliding Window(线段树求最值)

    链接 裸线段树 这题时间卡的挺棒 #include <iostream> #include<cstdio> #include<cstring> #include&l ...

  2. 裸的单调队列-poj-2823-Sliding Window

    题目链接: http://poj.org/problem?id=2823 题目意思: 给n个数,求连续区间长度为k的最大和最小值. 解题思路: 裸的单调队列不解释,用两个队列保存. 代码: #incl ...

  3. 【单调队列】POJ2823-Sliding Window

    单调队列经典题之一. [思路] 设置两个单调队列分别记录最大值和最小值.对于每一个新读入的数字,进行两次操作(对于求最大值和最小值中的某一个而言),一是若队首不在滑窗范围内则删去:二是删去队末比当前值 ...

  4. poj2823Sliding Window——单调队列

    题目:http://poj.org/problem?id=2823 单调队列模板. 代码如下: #include<iostream> #include<cstdio> usin ...

  5. [虾扯蛋] android界面框架-Window

    从纯sdk及framwork的角度看,android中界面框架相关的类型有:Window,WindowManager,View等.下面就以这几个类为出发点来概览下安卓开发的"界面架构&quo ...

  6. JS判断鼠标进入容器方向的方法和分析window.open新窗口被拦截的问题

    1.鼠标进入容器方向的判定 判断鼠标从哪个方向进入元素容器是一个经常碰到的问题,如何来判断呢?首先想到的是:获取鼠标的位置,然后经过一大堆的if..else逻辑来确定.这样的做法比较繁琐,下面介绍两种 ...

  7. 谈谈document.ready和window.onload的区别

    在Jquery里面,我们可以看到两种写法:$(function(){}) 和$(document).ready(function(){}) 这两个方法的效果都是一样的,都是在dom文档树加载完之后执行 ...

  8. X Window 的奥秘

    大名鼎鼎的 X Window 大家肯定不陌生.都知道它是 Unix/Linux 下面的窗口系统,也都知道它基于 Server/Clinet 架构.在网上随便搜一搜,也可以找到不少 X Window 的 ...

  9. Android Starting Window(Preview Window)

    当打开一个Activity时,如果这个Activity所属的应用还没有在运行,系统会为这个Activity所属的应用创建一个进程,但进程的创建与初始化都需要时间,在这个动作完成之前系统要做什么呢?如果 ...

随机推荐

  1. f2fs源码解析(五) node管理结构梳理

    node是f2fs重要的管理结构, 它非常重要! 系统挂载完毕后, 会有一个f2fs_nm_info结构的node管理器来管理node的分配. f2fs_nm_info中最让人疑惑的是几颗基数树: s ...

  2. 02SpringMvc_springmvc快速入门小案例(XML版本)

    这篇文章中,我们要写一个入门案例,去整体了解整个SpringMVC. 先给出整个项目的结构图:

  3. 你会在C#的类库中添加web service引用吗?

    本文并不是什么高深的文章,只是VS2008应用中的一小部分,但小部分你不一定会,要不你试试: 本人对于分布式开发应用的并不多,这次正好有一个项目要应用web service,我的开发环境是vs2008 ...

  4. /etc/profile和~/.bash_profile的区别

    /etc/profile是全局的,是私有的 /etc/profile用于整个系统所有用户, ~/.bash_profile, ~/.profile和~/.bashrc 用于各个用户,这里的" ...

  5. 【Asp.Net】Asp.Net CommandName作用

    数据绑定控件的模板中 CommandName 属性以下属性值会触发特定的事件: Cancel(取消) Delete(删除) Select(选择) Edit(编辑) Insert(插入) Update( ...

  6. TDD开发感悟

    由于公司要实现TDD形式的开发,所以准备了一下,准备在后续的项目中,投入到TDD的怀抱中. 在找一些参考书目的过程中,偶遇<测试驱动开发的艺术>这本书,书中的编码为JAVA派系,但是书的内 ...

  7. 2016科幻惊悚《第五波》HD720P.中英双字

    导演: J·布莱克森编剧: 苏珊娜·格兰特 / 阿齐瓦·高斯曼 / 杰夫·皮克纳 / 瑞克·杨西主演: 科洛·莫瑞兹 / 尼克·罗宾森 / 朗·里维斯顿 / 玛姬·丝弗 / 亚历克斯·罗伊 / 更多. ...

  8. ListView中多个EditText设置焦点 多次点击异常报错

    08-17 18:23:09.825: ERROR/AndroidRuntime(1608): FATAL EXCEPTION: main 08-17 18:23:09.825: ERROR/Andr ...

  9. 【MyEclipse 2015】 逆向破解实录系列【终】(纯研究)

    声明 My Eclipse 2015 程序版权为Genuitec, L.L.C所有. My Eclipse 2015 的注册码.激活码等授权为Genuitec, L.L.C及其付费用户所有. 本文只从 ...

  10. Unity3D UGUI中ScrollRect的一些知识点

    需求 这几天在公司里,项目需要将游戏游戏中的2D城堡界面在拉动的时候显示出3D的拉近效果.当时是在Cocos2d-x下实现的.回家的时候自己重新用Unity实现的了一遍. 虽然现在Unity已经到了5 ...