Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 53086   Accepted: 15227
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

模拟滑动区间,用单调队列维护最值即可。本来正常做法是用单调队列存下标,然而蠢蠢的我又多开了俩数组……这样效率会降低,好在还是过了。

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
const int mxn=;
int n,k;
int num;
int qmx[mxn],qmi[mxn];
int lmx[mxn],lmi[mxn];
int ansmx[mxn];
int main(){
scanf("%d%d",&n,&k);
int i,j; int h1=,t1=;
int h2=,t2=;
for(int cnt=;cnt<=n;cnt++){
scanf("%d",&num);
//max
while(h1<=t1 && num>=qmx[t1])t1--;
qmx[++t1]=num;
lmx[t1]=cnt;//下标 //min
while(h2<=t2 && num<=qmi[t2])t2--;
qmi[++t2]=num;
lmi[t2]=cnt; if(lmx[t1]-lmx[h1]>=k)h1++;
if(lmi[t2]-lmi[h2]>=k)h2++;
// printf("test: h1:%d t1:%d\n",h1,t1);
ansmx[cnt]=qmx[h1];
if(cnt>=k)printf("%d ",qmi[h2]);
}
printf("\n");
for(i=k;i<=n;i++)printf("%d ",ansmx[i]); return ;
}

POJ2823 Sliding Window的更多相关文章

  1. POJ2823 Sliding Window (单调队列)

    POJ2823 Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 38342   Accepte ...

  2. codevs4373 窗口==poj2823 Sliding Window

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 53676   Accepted: 15399 ...

  3. POJ2823 Sliding Window(单调队列)

    单调队列,我用deque维护.这道题不难写,我第二次写单调队列,1次AC. -------------------------------------------------------------- ...

  4. POJ2823 Sliding Window(单调队列)

    题目要输出一个序列各个长度k的连续子序列的最大值最小值. 多次RMQ的算法也是能过的,不过单调队列O(n). 这题,队列存元素值以及元素下标,队尾出队维护单调性然后入队,队首出队保持新元素下标与队首元 ...

  5. [POJ2823]Sliding Window 滑动窗口(单调队列)

    题意 刚学单调队列的时候做过 现在重新做一次 一个很经典的题目 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗 ...

  6. poj2823 Sliding Window luogu1886 滑动窗口 单调队列

    模板题 #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...

  7. POJ2823 Sliding Window【双端队列】

    求连续的k个中最大最小值,k是滑动的,每次滑动一个 用双端队列维护可能的答案值 假设要求最小值,则维护一个单调递增的序列 对一開始的前k个,新增加的假设比队尾的小.则弹出队尾的,直到新增加的比队尾大. ...

  8. [POJ2823] Sliding Window 「单调队列」

    我们从最简单的问题开始: 给定一个长度为N的整数数列a(i),i=0,1,...,N-1和窗长度k. 要求:   f(i) = max{ a(i-k+1),a(i-k+2),..., a(i) },i ...

  9. LeetCode题解-----Sliding Window Maximum

    题目描述: Given an array nums, there is a sliding window of size k which is moving from the very left of ...

随机推荐

  1. js原生选项卡(自动播放无缝滚动轮播图)二

    今天分享一下自动播放轮播图,自动播放轮播图是在昨天分享的轮播图的基础上添加了定时器,用定时器控制图片的自动切换,函数中首先封装一个方向的自动播放工能的小函数,这个函数中添加定时器,定时器中可以放向右走 ...

  2. pycharm简单使用

    http://blog.csdn.net/chenggong2dm/article/details/9365437

  3. Windows7+VS2012下OpenGL 4的环境配置

    系统环境 Windows 7 Ultimate x64,Visual Studio Ultimate 2012 Update 4,和一块支持OpenGL 4.x的显卡. 准备工作 首先用GPU Cap ...

  4. [转]在 Eclipse 中嵌入 NASA World Wind Java SDK

    使用此开源 SDK 开发 GIS 应用程序 NASA 开发的开源 World Wind Java (WWJ) SDK 为地理信息系统(Geographic Information Systems,GI ...

  5. iBatis.Net(C#)数据库查询

    引用请注明http://www.cnblogs.com/13590/archive/2013/03/14/2958735.html  摘要:查询是数据库SQL语言的核心,本文介绍了通过iBatis.N ...

  6. MVC中利用ActionFilterAttribute过滤关键字

    在开发过程中,有时候会对用户输入进行过滤,以便保证平台的安全性.屏蔽的方法有很多种,但是今天我说的这种主要是利用MVC中的ActionFilterAttribute属性来实现.由于MVC天然支持AOP ...

  7. ios 指南针

    指南针图片  IBOutlet UIImageView *compassView 1 #import "ViewController.h" #import <CoreLoca ...

  8. 在coding上添加ssh-key

    第一步:检查有没有ssh-key 第二步:生成ssh-key 第三步:添加到coding上或者Github上. ls -al ~/.ssh ssh-keygen -t rsa -C "you ...

  9. Jenkins入门系列之——00答疑解惑

    写在最前的总结:Jenkins其实就是一个工具,这个工具的作用就是调用各种其他的工具来达成你的目的.比如你要获取Subversion上最新的源代码,Jenkins会去调用SVNKIT(插件的核心Jar ...

  10. 关于json 与 Request Header 的Content-Type 一些关系。

    由于最近遇到关于,ashx文件ajax解析参数的问题.查询网上很多资料后,已经解决. 鉴于网上已经足够多的,关于这个问题的文章.大部分内容来自互联网,我这里只是做一些整理和记录.特此说明并非原创. C ...