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. openstack常规操作命令梳理

    之前顺利部署了openstack+kvm的虚拟化云平台管理环境,其中,涉及到不少openstack操作命令,记录如下: 查看rabbitmq 队列[root@openstack ~]#rabbitmq ...

  2. 无法解析此远程名称: 'www.***.com' 解决办法 请求因 HTTP 状态 417 失败

    今天在做接口开发时,遇到了一个异常:无法解析此远程名称: 'www.***.com'.我的网站一直是运行正常的,从昨天开始出现异常,用户可以使用,但我的服务器怎么也无法实现对数据库的更新. 分析原因: ...

  3. Xcode6与Xcode5中沙盒的变动以及偏好设置目录的变动

    1.Xcode6模拟器路径与Xcode5模拟器路径对比: (1)Xcode5中模拟器路径为:/Users/用户名/Library/Application Support/iPhone Simulato ...

  4. 最近火到不行的微信小程序的常识

    满网都是微信小程序,技术dog们不关注都不行了.先别忙着去学怎么开发小程序,先纠正一下你对微信小程序的三观吧~~~~ 小程序目前被炒得沸沸扬扬,无数媒体和企业借机获取阅读流量. 这再次证明一点,微信想 ...

  5. (转载)Apache下error.log文件太大的处理

    偶尔发现Apache下的错误日志非常的大,有5G多,先停止Apache服务的所有进程,最简单就是输命令:net stop apache2.4,然后删除 Apache/logs/目录下的 error.l ...

  6. Asp.net设计模式笔记之一:理解设计模式

    GOF设计模式著作中的23种设计模式可以分成三组:创建型(Creational),结构型(Structural),行为型(Behavioral).下面来做详细的剖析. 创建型 创建型模式处理对象构造和 ...

  7. SpringMVC数据验证

    SpringMVC数据验证——第七章 注解式控制器的数据验证.类型转换及格式化——跟着开涛学SpringMVC 资源来自:http://jinnianshilongnian.iteye.com/blo ...

  8. 20135316王剑桥 linux第二周课实验笔记

    Linux中命令格式为: command [options选项] [arguments参数] //中括号代表是可选的,即有些命令不需要选项也不需要参数 ls或ls .显示是当前目录的内容,这里“.”就 ...

  9. RESideMenu左右半侧滑的功能实现,主视图会和状态栏(StatusBar)不会随着一起滑动

    具体demo去github下载,这里不详细描述

  10. 使用线程池模拟处理耗时任务,通过websocket提高用户体验

    前言 在文章开始之前,询问一下大家平时工作中后端处理批量任务(耗时任务)的时候,前端是如何告知用户任务的执行情况的? 楼主对这个问题想了下,决定使用websokect将这一过程展现给用户. 于是就有了 ...