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. Python-执行系统命令

    执行系统命令 os.system os.spawn* os.popen popen2.* commands.* 后面三个已经废弃,以上执行shell命令的相关的模块和函数的功能均在subprocess ...

  2. 四、Protocol 类似java的接口

    概念:是一系列方法的列表,其中声明的方法可以被任意类实现.这种模式称为代理.和JAVA接口不同的是,Protocol可以不用被实现所有的方法. 使用场景:想要监听一些按钮的操作 1声明一个协议 //& ...

  3. WPF基础——Application

    一.Application介绍 WPF中的Application对象用来进行一些全局的行为和操作,并且每个 Domain (应用程序域)中仅且只有一个 Application 实例存在.WPF App ...

  4. customized English word breaker for sql server 2008

    Open the Registry Editor, by: Clicking Start, and clicking Run. In the Run dialog box, in the Open b ...

  5. 利用ViewHolder优化自定义Adapter的典型写法

    1 public class MarkerItemAdapter extends BaseAdapter { private Context mContext = null; private List ...

  6. C++哈弗曼编码

    // haffman.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using name ...

  7. 结合C++和GDAL实现shapefile(shp)文件的读取

    工具:vs2012+GDAL 2.0 数据:中国省界SHP文件bou2_4p.shp   可点击下载 包含头文件: #include "ogrsf_frmts.h" 代码: int ...

  8. iOS 自定义控件开发(中)

    <iOS 自定义控件开发(上)> <iOS 自定义控件开发(中)> 接上篇iOS自定义控件开发之后,我们尝试另外一种. 在Xcode的右边,会看到如下的图 其中,上面有一个:C ...

  9. [C++基础]一个比较常用的配置文件/初始化文件读取程序

    在编程中,我们经常会遇到一些配置文件或初始化文件.这些文件通常后缀名为.ini或者.conf,可以直接用记事本打开.里面会存储一些程序参数,在程序中直接读取使用.例如,计算机与服务器通信,服务器的ip ...

  10. sql server 2008 数据库的完整性约束

    一.数据库完整性概述   1.数据库的完整性:   ①数据库的完整性是指数据的正确性和相容性 ②数据库完整性是防止不合语义或不正确的数据进入数据库 ③完整性体现了是否真实地反映现实世界   例:  学 ...