Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 48608   Accepted: 14047
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
 #include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std; int a[],q[],p[],mi[],ma[];
int n,k; void get_min()
{
int i,j;
int head=,tail=;
for(i=;i<k-;i++)
{
while(head<=tail && q[tail]>=a[i])
tail--;
q[++tail]=a[i];
p[tail]=i;
} for(;i<n;i++)
{
while(head<=tail && q[tail]>=a[i])
tail--;
q[++tail]=a[i];
p[tail]=i;
while(p[head]<i-k+)
{
head++;
}
mi[i-k+]=q[head];
}
} void get_max()
{
int i,j;
int head=,tail=;
for(i=;i<k-;i++)
{
while(head<=tail && q[tail]<=a[i])
tail--;
q[++tail]=a[i];
p[tail]=i;
}
for(;i<n;i++)
{
while(head<=tail && q[tail]<=a[i])
tail--;
q[++tail]=a[i];
p[tail]=i;
while(p[head]<i-k+)
head++;
ma[i-k+]=q[head];
}
} int main()
{
int i,j;
scanf("%d %d",&n,&k);
for(i=;i<n;i++)
{
scanf("%d",&a[i]);
}
get_max();
get_min();
for(i=;i<n-k;i++)
{
printf("%d ",mi[i]);
}
printf("%d\n",mi[n-k]);
for(i=;i<n-k;i++)
{
printf("%d ",ma[i]);
}
printf("%d\n",ma[n-k]);
return ;
}

单调队列 hdu2823的更多相关文章

  1. BestCoder Round #89 B题---Fxx and game(单调队列)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945     问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路:  B ...

  2. 单调队列 && 斜率优化dp 专题

    首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...

  3. FZU 1914 单调队列

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...

  4. BZOJ 1047 二维单调队列

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1047 题意:见中文题面 思路:该题是求二维的子矩阵的最大值与最小值的差值尽量小.所以可以考 ...

  5. 【BZOJ3314】 [Usaco2013 Nov]Crowded Cows 单调队列

    第一次写单调队列太垃圾... 左右各扫一遍即可. #include <iostream> #include <cstdio> #include <cstring> ...

  6. BZOJ1047: [HAOI2007]理想的正方形 [单调队列]

    1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2857  Solved: 1560[Submit][St ...

  7. hdu 3401 单调队列优化DP

    Trade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  8. 【转】单调队列优化DP

    转自 : http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列是一种严格单调的队列,可以单调递增,也可以单调递减.队 ...

  9. hdu3530 单调队列

    Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

随机推荐

  1. MyEclipse不能重新发布的解决方案

    myeclipse2015已发布项目上右键clear不重新发布的解决方案: 该项目在server.xml中没有设置自动发布reloadable="true"

  2. 大量查询SQL语句 实例

    1.查看表结构语句:DESC   表名   2.查询所有列:select  *  from  表名   3.查询指定列:select  字段名  form  表名   4.查询指定行:SELECT * ...

  3. win7安装virtualbox遇到的问题

    今天用台式机的时候想装个virtualbox跑centos做测试用,结果centos始终装不上,vbox一直提示无法开启任务.重装vbox,以及手动点击安装xxx.inf文件,都不行. 以前用的win ...

  4. Ajax 无刷新上传文件插件 uploadify 的使用

    在表单中无法直接使用 Ajax 上传文件,解决的思路可以是使用插件无刷新地上传文件,返回文件上传后的地址,然后把该地址作为 Ajax 的参数传递给服务器端进行数据库处理.可以使用 uploadify ...

  5. 《UML大战需求分析》阅读笔记02

    软件应能真正的对客户的工作提供帮助,只有客户使用并真正对其有用才能说我们做的软件成功了.客户从开始提出需求时到后期的工作过程中,需求总是不停变化的,所以这就需要需求人员对需求领先于客户.所以活用UML ...

  6. You and Your Research(Chinese)

    转自:http://lyxh-2003.iteye.com/blog/434014 这是大科学家Richard Hamming的著名讲演,于1986年在贝尔通讯研究中心给200多名Bellcore的科 ...

  7. 使用 U盘 重装 Mac OSX

    一.制作 U 盘系统启动盘 1.从 App Store 上下载 OS Application.(这里需要注意,取消下载完的自动更新,并存储下这个 OS.Application 文件,因为系统更新完后, ...

  8. Redis配置文件(redis.conf)说明

    Redis 配置 Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf. 你可以通过 CONFIG 命令查看或设置配置项. 语法3> Redis CONFIG 命令 ...

  9. iOS:最详细的创建CocoaPods私有库教程

    一.感慨 说实话,创建这个CocoaPods私有库,我愣是搞了两个星期,创建的过程中,自己的感情波动是这样的:激情四射---->有点困惑----->极度困惑----->有点失望--- ...

  10. Inside Flask - signal 信号机制

    Inside Flask - signal 信号机制 singal 在平常的 flask web 开发过程中较少接触到,但对于使用 flask 进行框架级别的开发时,则必须了解相关的工作机制.flas ...