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

4373 窗口

 时间限制: 1 s
 空间限制: 256000 KB
 题目等级 : 黄金 Gold
 查看运行结果
 
 
 
 
 
题目描述 Description

给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表:

Window position Min value  Max 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

你的任务是找出窗口在各位置时的max value, min value.

输入描述 Input Description

第1行n,k,第2行为长度为n的数组

输出描述 Output Description

2行

第1行每个位置的min value

第2行每个位置的max value

样例输入 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

数据范围及提示 Data Size & Hint

数据范围:20%: n<=500; 50%: n<=100000;100%: n<=1000000;

分类标签 Tags 点此展开

 
暂无标签
 

2017-03-27

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=1e6+;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,a[N],q[N],id[N];
int main(){
n=read();m=read();
for(int i=;i<=n;i++) a[i]=read();
int h=,t=;
for(int i=;i<m;i++){
while(h<t&&i-id[h]>=m) h++;
while(h<t&&q[t-]>a[i]) t--;
q[t]=a[i];id[t++]=i;
}
for(int i=m;i<=n;i++){
while(h<t&&i-id[h]>=m) h++;
while(h<t&&q[t-]>a[i]) t--;
q[t]=a[i];id[t++]=i;
if(h<t) printf("%d ",q[h]);
}
putchar('\n');
h=,t=;
for(int i=;i<m;i++){
while(h<t&&i-id[h]>=m) h++;
while(h<t&&q[t-]<a[i]) t--;
q[t]=a[i];id[t++]=i;
}
for(int i=m;i<=n;i++){
while(h<t&&i-id[h]>=m) h++;
while(h<t&&q[t-]<a[i]) t--;
q[t]=a[i];id[t++]=i;
if(h<t) printf("%d ",q[h]);
}
return ;
}
AC代码(裸的单调队列)c++提交
#include<cstdio>
using namespace std;
inline int read(){
register int x=;bool f=;
register char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return f?x:-x;
}
const int N=1e6+;
int n,k,a[N],num[N],q[N];
int main(){
n=read();k=read();
for(int i=;i<=n;i++) a[i]=read();
int l=,r=;
for(int i=;i<k;i++){
for(;l<=r&&i-num[l]>=k;l++);
for(;l<=r&&a[i]<q[r];r--);
q[++r]=a[i];num[r]=i;
}
for(int i=k;i<=n;i++){
for(;l<=r&&i-num[l]>=k;l++);
for(;l<=r&&a[i]<q[r];r--);
q[++r]=a[i];num[r]=i;
printf("%d ",q[l]);
}
putchar('\n');
l=,r=;
for(int i=;i<k;i++){
for(;l<=r&&i-num[l]>=k;l++);
for(;l<=r&&a[i]>q[r];r--);
q[++r]=a[i];num[r]=i;
}
for(int i=k;i<=n;i++){
for(;l<=r&&i-num[l]>=k;l++);
for(;l<=r&&a[i]>q[r];r--);
q[++r]=a[i];num[r]=i;
printf("%d ",q[l]);
}
return ;
}

codevs4373 窗口==poj2823 Sliding Window的更多相关文章

  1. POJ2823 Sliding Window (单调队列)

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

  2. 滑动窗口(Sliding Window)技巧总结

    什么是滑动窗口(Sliding Window) The Sliding Problem contains a sliding window which is a sub – list that run ...

  3. [Swift]LeetCode239. 滑动窗口最大值 | Sliding Window Maximum

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

  4. 数据流滑动窗口平均值 · sliding window average from data stream

    [抄题]: 给出一串整数流和窗口大小,计算滑动窗口中所有整数的平均值. MovingAverage m = new MovingAverage(3); m.next(1) = 1 // 返回 1.00 ...

  5. 【LeetCode】480. 滑动窗口中位数 Sliding Window Median(C++)

    作者: 负雪明烛 id: fuxuemingzhu 公众号: 每日算法题 本文关键词:LeetCode,力扣,算法,算法题,滑动窗口,中位数,multiset,刷题群 目录 题目描述 题目大意 解题方 ...

  6. POJ2823 Sliding Window

    Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 53086   Accepted: 15227 Case Time Limi ...

  7. POJ2823 Sliding Window(单调队列)

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

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

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

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

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

随机推荐

  1. OLEVARIANT的替代——FIREDAC的TFDJSONDataSets和TFDJSONDeltas

    OLEVARIANT——这个COM的序列格式,也是DATASNAP已使用了20年的序列格式,在20年以后的今天,终于有了它的替代者:FIREDAC的TFDJSONDataSets和TFDJSONDel ...

  2. Linux性能监测

    1.Linux性能监测:监测目的与工具介绍 看了某某教程.读了某某手册,按照要求改改某些设置.系统设定.内核参数就认为做到系统优化的想法很傻很天真:)系统优化是一项复杂.繁琐.长期的工作,优化前需要监 ...

  3. [每日一题] 11gOCP 1z0-053 :2013-10-11 Flashback Data Archive属性.........................43

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/12656897 正确答案:BD 闪回数据归档请参考:http://blog.csdn.net ...

  4. jquery页面加载的时候加载函数

    http://blog.csdn.net/tjcyjd/article/details/6713474 jQuery 页面加载初始化的方法有3种 ,页面在加载的时候都会执行脚本,应该没什么区别,主要看 ...

  5. 【JDBC】预编译SQL与防注入式攻击

    在JDBC编程中,常用Statement.PreparedStatement 和 CallableStatement三种方式来执行查询语句,其中 Statement 用于通用查询, PreparedS ...

  6. .NET Reflector插件FileDisassembler还原源码

    NET Reflector,它是一个类浏览器和反编译器,可以分析程序集并向您展示它的所有秘密..NET 框架向全世界引入了可用来分析任何基于 .NET 的代码(无论它是单个类还是完整的程序集)的反射概 ...

  7. How Much Work Does it Take to be a Successful Mathematician?

    http://mathoverflow.net/questions/9799/how-much-work-does-it-take-to-be-a-successful-mathematician# ...

  8. [前端JS学习笔记]JavaScript 数组

    一.JavaScript数组的奇葩 大多数语言会要求数组的元素是同个类型, 但是JavaScript允许数组元素为多种类型. var arr = ["羽毛球", 666, {&qu ...

  9. IOS6 字体高亮显示

    ios6之前在一个字符串中如果也让某个字体高亮或者特殊显示(如: 关注[ ]),需要用单独一个的标签进行显示,或者利用CoreText进行字体绘绘制,非常麻烦: 现在IOS6 中TextView,la ...

  10. HDU 4293 Groups (线性dp)

    OJ题目:click here~~ 题目分析:n个人分为若干组 , 每一个人描写叙述其所在的组前面的人数和后面的人数.求这n个描写叙述中,最多正确的个数. 设dp[ i ] 为前i个人的描写叙述中最多 ...