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. ASP.NET MVC3 Model验证总结(转)

    推荐:   ASP.NET MVC的Model元数据与Model模板:预定义模板 http://www.cnblogs.com/artech/archive/2012/05/02/model-meta ...

  2. Ubuntu优化-修改启动级别

    一 修改Ubuntu启动级别 sudo apt-get install sysv-rc-conf 执行: sysv-rc-conf 打x的表示开机启动. 二 启动级别 Ubuntu默认启动级别为2 r ...

  3. C语言 动态库简单开发

    动态库项目 //简单的动态库开发----报文发送 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib. ...

  4. C#中成员初始化顺序

    http://blog.csdn.net/huangcailian/article/details/25958967 一.成员初始化整体顺序 1.成员赋值初始化先于构造函数: 2.成员赋值初始先从子类 ...

  5. pandas groupby

    pandas.DataFrame.groupby DataFrame.groupby(by=None, axis=0, level=None, as_index=True, sort=True, gr ...

  6. 基于EventAggregator的事件发布及订阅

    EventAggregator简介 EventAggregator是Prism中专门处理ViewModel与ViewModel之间事件传递的类对象,它提供了针对事件的发布方法和订阅方法,所以可以非常方 ...

  7. 一个因为粗心的Bug

    /** * 数据绑定,分页显示 */ private void updataMenu(final EditText search) { if(listwz==null) { return; } pag ...

  8. 20135202闫佳歆--week 7 深入理解计算机系统第七章--读书笔记

    参见上学期的学习笔记: http://www.cnblogs.com/20135202yjx/p/4836058.html

  9. 一个基于.NET平台的自动化/压力测试系统设计简述

    AutoTest系统设计概述 AutoTest是一个基于.NET平台实现的自动化/压力测试的系统,可独立运行于windows平台下,支持分布式部署,不需要其他配置或编译器的支持.(本质是一个基于协议的 ...

  10. web前端开发教程系列-2 - 前端开发书籍分享

    目录: 前言 一. CSS 二. JavaScript 三. jQuery 四. 后记   前言 前端书籍在每个商城或书架上面都是琳琅满目,很多初学者又不能很好的判断书的质量或层次.因为今天给同学们分 ...