Sliding Window

Time Limit:12000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

An array of size n ≤ 10 6 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
題意 :
  给定一个数字序列,与每次所能看见的序列长度m即窗户长度,从左向右依次移动窗户,输出每次所能看到的序列长度的最大值与最小值,直到序列结束.
思路:
  以求最小值为例,
  1.构造递增的单调队列,即队尾元素需要小于当前元素,否则队尾元素出队.
  2.首先,将前 m-1 个元素按照单调队列原则进队,并记录队列中的每个元素在数组中的下标;
   其次,将剩余的元素依次进队,以 第i元素为例子,将第i元素与队尾比较,若是小于队尾,则队尾出队,否则当前元素进队,此时队列是单调递增有序的,
   再之,将队头元素的下标与当前元素的下标比较,若 当前元素下标 - 队头元素下标<=m-1,则队头元素便为最小值,否则队头出队.
  3.将剩余元素依次进行步骤2,便得到所求的所有最小值,求最大值亦然.
AC代码分析 (请用C++提交,否则Time Limit Exceeded):
 #include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include<string.h>
#include<stack>
#include<set>
#include <queue>
using namespace std;
int a[];
//队列中各元素的下标
int p[];
//各个区间的最大值
int max1[];
//各个区间的最小值
int min1[];
//数组模拟递增队列
int qmax[];
//数组模拟递减队列
int qmin[];
//队头,队尾
int head,tail;
int main()
{
int n,m,i,t;
while(~scanf("%d%d",&n,&m))
{
for( i = ; i<=n; i++) scanf("%d",a+i);
//队头队尾初始化
head = ;
tail = ;
t = ;
//前m-1个元素进队列
for( i = ; i<=m-; i++)
{
while(head<=tail&&qmin[tail]>=a[i]) tail--;
qmin[++tail] = a[i];
//队列中各元素的下标
p[tail] = i; }
//求所有的最小值
for(; i<=n; i++)
{
//进队
while(head<=tail&&qmin[tail]>=a[i]) tail--;
qmin[++tail] = a[i];
p[tail] = i;
//判断对头是否在当前范围内
while(i-p[head]>m-)
head++;
min1[t++] = qmin[head];
}
head = ;
tail = ;
t = ;
//求所有的最大值
for( i = ; i<=m-; i++)
{
while(head<=tail&&qmax[tail]<=a[i]) tail--;
qmax[++tail] = a[i];
p[tail] = i;
}
for(; i<=n; i++)
{
//进队
while(head<=tail&&qmax[tail]<=a[i]) tail--;
qmax[++tail] = a[i];
p[tail] = i;
//判断对头是否在当前范围内
while(i-p[head]>m-)
head++;
max1[t++] = qmax[head];
}
for( i = ; i<t; i++)
{
if(i == )
printf("%d",min1[i]);
else
printf(" %d",min1[i]);
}
printf("\n");
for( i = ; i<t; i++)
{
if(i == )
printf("%d",max1[i]);
else
printf(" %d",max1[i]);
} }
return ;
}
本文为个人随笔,如有不当之处,望各位大佬多多指教.
若能为各位博友提供小小帮助,不胜荣幸.

POJ--2823--Sliding Window----单调队列问题的更多相关文章

  1. POJ 2823 Sliding Window + 单调队列

    一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1)   从队首删除 (2)   从队尾删除 (3)   从队尾插入 (4)   ...

  2. poj 2823 Sliding Window (单调队列入门)

    /***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...

  3. POJ 2823 Sliding Window (单调队列)

    单调队列 加了读入挂比不加更慢.... 而且这份代码要交c++ 有大神G++跑了700ms..... orzorzorz #include<iostream> #include<cs ...

  4. poj 2823 Sliding Windows (单调队列+输入输出挂)

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 73426   Accepted: 20849 ...

  5. POJ 2823 Sliding Window 题解

    POJ 2823 Sliding  Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...

  6. 洛谷P1886 滑动窗口(POJ.2823 Sliding Window)(区间最值)

    To 洛谷.1886 滑动窗口 To POJ.2823 Sliding Window 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每 ...

  7. POJ 2823 Sliding Window(单调队列入门题)

      Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 67218   Accepted: 190 ...

  8. POJ 2823 Sliding Window 【单调队列】

    题目链接:http://poj.org/problem?id=2823 题目大意:给出一组数,一个固定大小的窗体在这个数组上滑动,要求出每次滑动该窗体内的最大值和最小值. 这就是典型的单调队列,单调队 ...

  9. 【单调队列】poj 2823 Sliding Window

    http://poj.org/problem?id=2823 [题意] 给定一个长度为n的序列,求长度为k的滑窗内的最大值和最小值 [思路] 裸的单调队列 注意用C++提交,不然会T,orz我用G++ ...

  10. 题解报告:poj 2823 Sliding Window(单调队列)

    Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...

随机推荐

  1. UVA 1642 Magical GCD(gcd的性质,递推)

    分析:对于区间[i,j],枚举j. 固定j以后,剩下的要比较M_gcd(k,j) = gcd(ak,...,aj)*(j-k+1)的大小, i≤k≤j. 此时M_gcd(k,j)可以看成一个二元组(g ...

  2. Count Numbers(矩阵快速幂)

    Count Numbers 时间限制: 8 Sec  内存限制: 128 MB提交: 43  解决: 19[提交] [状态] [讨论版] [命题人:admin] 题目描述 Now Alice want ...

  3. 《剑指offer》【调整数组顺序使奇数位于偶数前面】(python版)

    题目描述: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分 思路: 我认真看了一下,题目应该是要求在原地调整,所以这里不能再 ...

  4. Mac 系统 + Chrome浏览器 网页前端出现中文文字反转或顺序错乱

    问题背景 React开发的系统,收到一个BUG反馈,*"号个人统计"文字不正确,应为"个人号统计"*. 收到BUG后,打开浏览器查验是什么情况,难道犯了最基本的 ...

  5. GMap.Net解决方案之在WinForm和WPF中使用GMap.Net地图插件的开发

    在做地理位置相关的开发时,总是面临高额地图引擎费用让大部分用户望而却步,加之地图数据又是天价,那么GMap.NET就是首选了,它本身就是开源免费,服务器可以在本地缓存,以后访问时就可以直接访问. 可以 ...

  6. java基础面试题:switch语句能否作用在byte上,能否作用在long上,能否作用在String上?

    package com.swift; public class Switch_Test { public static void main(String[] args) { /* * switch语句 ...

  7. 2017年9月11日 梁勇 java教材 编程练习题 第二章 2.15 键盘 读取两个点的坐标值(小数),控制台输出两点间距离。

    package com.swift; import java.util.Scanner; public class PToP { public static void main(String[] ar ...

  8. @property 关键字的区别详解

    @property 关键字的区别详解:assign 与weak. __block 与 __weak.strong 与copy的区别 字数911 阅读269 评论2 喜欢8 在开发中经常使用属性@pro ...

  9. 2018.10.30 NOIp模拟赛T2 数字对

    [题目描述] 小 H 是个善于思考的学生,现在她又在思考一个有关序列的问题.        她的面前浮现出一个长度为 n 的序列{ai},她想找出一段区间[L, R](1 <= L <= ...

  10. day13内置函数

    内置函数 一.三元表达式 def max2(x,y): if x>y: return x else: return y res=max2(10,11) print(res) 三元表达式仅应用于: ...