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. json转换数据后面参数要带ture,代码

    强大的PHP已经提供了内置函数:json_encode() 和 json_decode().很容易理解,json_encode()就是将PHP数组转换成Json.相反,json_decode()就是将 ...

  2. 20145337《Java程序设计》第八周学习总结

    20145337<Java程序设计>第八周学习总结 教材学习内容总结 15.1日志 15.1.1日志API简介 使用日志的起点是logger类,logger实例的创建有许多要处理的要素,必 ...

  3. js中的回调函数的理解和使用方法

    js中的回调函数的理解和使用方法 一. 回调函数的作用 js代码会至上而下一条线执行下去,但是有时候我们需要等到一个操作结束之后再进行下一个操作,这时候就需要用到回调函数. 二. 回调函数的解释 因为 ...

  4. centos桌面使用

    firefox添加flash插件 [root@bogon home]# cp libflashplayer.so /usr/lib64/mozilla/pl pl plugins/ plugins-w ...

  5. 有趣的BAT

    最近某个用到的第三方程序会产生很多日志文件在logs目录中,每天一个log文件,类似 2014-05-07001.log.日积月累这个目录文件数量非常多,手动清除还是比较麻烦的. 由于这个软件不是自己 ...

  6. UIAlertController 的使用

    IAlertController 同时替代了 UIAlertView 和 UIActionSheet,从系统层级上统一了 alert 的概念 —— 即以 modal 方式或 popover 方式展示. ...

  7. JS-011-颜色进制转换(RGB转16进制;16进制转RGB)

    在网页开发的时候,经常需要进行颜色设置,因而经常需要遇到进行颜色进制转换的问题,例如:RGB转16进制:16进制转RGB),前几天在测试的时候,发现网站的颜色进制转换某类16进制颜色(例如:#0000 ...

  8. [开源]基于STM32的录音播放装置

    这是帮一个同学做毕设做的,基本要求如下(有些指标看看就好,实际当然不需要,哈哈): (1)放大器1的增益为46dB,放大器2的增益为40dB,增益均可调:(2)带通滤波器:通带为300Hz-3.4kH ...

  9. RabbitMQ学习总结 第三篇:工作队列Work Queue

    目录 RabbitMQ学习总结 第一篇:理论篇 RabbitMQ学习总结 第二篇:快速入门HelloWorld RabbitMQ学习总结 第三篇:工作队列Work Queue RabbitMQ学习总结 ...

  10. 浏览器浏览记忆(history)几则技巧记录

    一般浏览记录模式 假设有三个页面, start.html, 通过点击start.html上的链接跳转到 first.html, 然后点击first.html上链接跳转到 second.html, 那么 ...