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. 如何处理CloudFoundry应用部署时遇到的254错误

    使用SAP云平台的CloudFoundry部署应用: 在cockpit遇到错误信息:instance: a0abe2b5-7623-4cf1-4c65-0c79, index: 0, exit_des ...

  2. [Rails学习之路]Rails文件结构与路由

    约定优于配置和RESTful是Ruby on Rails十分推崇的哲学.在一个默认的RESTful的Rails项目中,使用资源和HTTP动词来帮助组织项目. 假如有一个使用scaffold创建的Rai ...

  3. IOS UISwitch控件的基本使用

    * UISwitch继承自UIControl,因此也能像UIButton一样监听一些事件,比如状态改变事件* UISwitch可以通过拖线监听状态改变* UISwitch可以通过addTarget:. ...

  4. Using Autorelease Pool Blocks

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAut ...

  5. 踩坑日志!viser-ng的使用

    在ng-alian项目中使用viser图表库,在app.module中引用了viser-ng,然而,在具体的html项目中使用<v-chart>会报错,提示v-chart不是一个angul ...

  6. Luogu P5008 逛庭院

    题目传送门 我校神仙出的神仙题 \(\%\%\%\) 30分 找出所有有入度的点,排序,选前\(k\)个点,好了,30分到手. #include<iostream> #include< ...

  7. Redis学习记录(三)

    1.Redis集群的搭建 1.1redis-cluster架构图 架构细节: (1)所有的redis节点批次互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽. (2)节点的fail ...

  8. Access数据库远程连接的实用方法

    一般在远程文件夹开启文件共享即可通过像平常一样用连接字符串访问,注意共享的读写权限. 远程(如通过互联网)连接access数据库的示例: 首先,需要使用TCP/IP,ADO及XML(需要安装Micro ...

  9. linux网络编程之断点传输文件

    以下载链接"http://www.boa.org/boa-0.94.13.tar.gz"为例: 断点续传实验大概步骤: ===================== 1,使用geth ...

  10. 【思维题 经典模型】cf632F. Magic Matrix

    非常妙的经典模型转化啊…… You're given a matrix A of size n × n. Let's call the matrix with nonnegative elements ...