Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 55309   Accepted: 15911
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

Source

题意:
N个数一串,一个长度为K的窗口,窗口开始在最左边,每次向右移动一格,问每次窗口内最大值和最小值。
代码:
基本单调队列
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,k;
int a[];
int ddq[];
void min()
{
int head=,tail=;
for(int i=;i<k-;i++)
{
while(head<=tail&&a[ddq[tail]]>=a[i])
tail--;
tail++;
ddq[tail]=i;
}
for(int i=k-;i<n;i++)
{
while(head<=tail&&a[ddq[tail]]>=a[i])
tail--;
tail++;
ddq[tail]=i;
while(ddq[head]<i-k+)
head++;
printf("%d",a[ddq[head]]);
if(i==n-) printf("\n");
else printf(" "); }
}
void max()
{
int head=,tail=;
for(int i=;i<k-;i++)
{
while(head<=tail&&a[ddq[tail]]<=a[i])
tail--;
tail++;
ddq[tail]=i;
}
for(int i=k-;i<n;i++)
{
while(head<=tail&&a[ddq[tail]]<=a[i])
tail--;
tail++;
ddq[tail]=i;
while(ddq[head]<i-k+)
head++;
printf("%d",a[ddq[head]]);
if(i==n-)
printf("\n");
else printf(" ");
}
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(ddq,,sizeof(ddq));
memset(a,,sizeof(a));
for(int i=;i<n;i++)
scanf("%d",&a[i]);
min();
max();
}
return ;
}

POJ 2838 单调队列的更多相关文章

  1. Sliding Window POJ - 2823 单调队列模板题

    Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...

  2. caioj 1172 poj 2823 单调队列过渡题

    给定一个n个数的数列,从左至右输出每个长度为m的数列段内的最大数. 输入:第一行两个整数n和m( 1<= n <= 20 0000,m<=n).下来给出n个整数. 输出:一行一个整数 ...

  3. poj 3017 单调队列优化动态规划

    思路:dp[i]=min{dp[j]+max(num[j+1]...num[i])},其中sum[i]-sum[j]<=m. 那么我们需要用单调队列维护j到i的最大值. #include< ...

  4. poj 2373 单调队列优化背包

    思路:我们用单调队列保存2*b<=i-j<=2*a中的最大值.那么队列头就是最大值,如果队头的标号小于i-2*b的话,就出队,后面的肯定用不到它了. #include<iostrea ...

  5. poj 2823 单调队列

    思路:裸的单调队列. #include<iostream> #include<cstring> #include<cstdio> #include<algor ...

  6. POJ 3017 单调队列dp

    Cut the Sequence Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8764   Accepted: 2576 ...

  7. POJ 1821 单调队列+dp

    题目大意:有K个工人,有n个墙,现在要给墙涂色.然后每个工人坐在Si上,他能刷的最大范围是Li,且必须是一个连续子区间,而且必须过Si,他刷完后能获得Pi钱 思路:定义dp[i][j]表示前i个人,涂 ...

  8. POJ 2823 单调队列入门水题

    最最基础的单调队列题目.一个单增一个单减.还是可以借此好好理解一下单调队列的. #include <stdio.h> #include <string.h> #include ...

  9. POJ - 1821 单调队列优化DP + 部分笔记

    题意:n个墙壁m个粉刷匠,每个墙壁至多能被刷一次,每个粉刷匠要么不刷,要么就粉刷包含第Si块的长度不超过Li的连续墙壁(中间可不刷),每一块被刷的墙壁都可获得Pi的利润,求最大利润 避免重复粉刷: 首 ...

随机推荐

  1. HDU 4005 The war Tarjan+dp

    The war Problem Description   In the war, the intelligence about the enemy is very important. Now, o ...

  2. barabasilab-networkScience学习笔记5- Barabási-Albert 模型

    第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...

  3. 数字信号处理实验(一)——DTFT

    1.MATLAB自编绘图函数 function [] = signal_write(X,w,flag) % X:数据 % w:频率向量 magX=abs(X);angX=angle(X); realX ...

  4. Linux学习笔记(12)用户和用户组管理

    越是对服务器安全性要求高的服务器,越需要建立合理的用户权限等级制度和服务器操作规范.在Linux中主要是通过用户配置文件来查看和修改用户信息. 1 用户信息文件 (1)用户信息文件/etc/passw ...

  5. LoadRunner 一些快捷键

    F4   Runtime Setting F5   运行 Ctrl Alt C  注释 Ctrl Alt U  取消注释 Tab  缩进 Shift Tab  取消缩进 Ctrl L  打开参数管理对 ...

  6. 制作caffe中的test.txt和val.txt

    find -name *.jpeg |cut -d '/' -f2-3> train.txt(图片在当前文件夹) find train/dog -name *.JPEG |cut -d '/' ...

  7. Hibernate,一对一外键单向 记录。Timestamp 的一个坑。

    首先是2张表 表A: 表B: 其中表B中的FormBaseId对应表A中的SubjectID. 数据库中没有设置外键关系. 下面是2个对应的实体 package questionnaire.model ...

  8. JetS3t使用说明

    http://blog.csdn.net/hitmediaman/article/details/6636402

  9. Python基础11- 函数之自定义函数

    自定义函数语法结构:def fun1([x],[y],....): 语句1 语句2 使用def语句来定义函数,在def后依次写出函数名.小括号.参数(可无).冒号,然后缩进写函数体 1.无参函数:de ...

  10. Learning storm book 笔记8-Log Processing With Storm

    有代码的书籍看起来就是爽,看完顺便跑个demo,感觉很爽! 场景分析 主要是利用apache的访问日志来进行分析统计 如用户的IP来源,来自哪个国家或地区,用户使用的Os,浏览器等信息,以及像搜索的热 ...