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

  1. 8 3
  2. 1 3 -1 -3 5 3 6 7

Sample Output

  1. -1 -3 -3 -3 3 3
  2. 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

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. using namespace std;
  5. const int N=1e6+;
  6. inline int read(){
  7. int x=,f=;char ch=getchar();
  8. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  9. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  10. return x*f;
  11. }
  12. int n,m,a[N],q[N],id[N];
  13. int main(){
  14. n=read();m=read();
  15. for(int i=;i<=n;i++) a[i]=read();
  16. int h=,t=;
  17. for(int i=;i<m;i++){
  18. while(h<t&&i-id[h]>=m) h++;
  19. while(h<t&&q[t-]>a[i]) t--;
  20. q[t]=a[i];id[t++]=i;
  21. }
  22. for(int i=m;i<=n;i++){
  23. while(h<t&&i-id[h]>=m) h++;
  24. while(h<t&&q[t-]>a[i]) t--;
  25. q[t]=a[i];id[t++]=i;
  26. if(h<t) printf("%d ",q[h]);
  27. }
  28. putchar('\n');
  29. h=,t=;
  30. for(int i=;i<m;i++){
  31. while(h<t&&i-id[h]>=m) h++;
  32. while(h<t&&q[t-]<a[i]) t--;
  33. q[t]=a[i];id[t++]=i;
  34. }
  35. for(int i=m;i<=n;i++){
  36. while(h<t&&i-id[h]>=m) h++;
  37. while(h<t&&q[t-]<a[i]) t--;
  38. q[t]=a[i];id[t++]=i;
  39. if(h<t) printf("%d ",q[h]);
  40. }
  41. return ;
  42. }
AC代码(裸的单调队列)c++提交
  1. #include<cstdio>
  2. using namespace std;
  3. inline int read(){
  4. register int x=;bool f=;
  5. register char ch=getchar();
  6. while(ch<''||ch>''){if(ch=='-')f=;ch=getchar();}
  7. while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
  8. return f?x:-x;
  9. }
  10. const int N=1e6+;
  11. int n,k,a[N],num[N],q[N];
  12. int main(){
  13. n=read();k=read();
  14. for(int i=;i<=n;i++) a[i]=read();
  15. int l=,r=;
  16. for(int i=;i<k;i++){
  17. for(;l<=r&&i-num[l]>=k;l++);
  18. for(;l<=r&&a[i]<q[r];r--);
  19. q[++r]=a[i];num[r]=i;
  20. }
  21. for(int i=k;i<=n;i++){
  22. for(;l<=r&&i-num[l]>=k;l++);
  23. for(;l<=r&&a[i]<q[r];r--);
  24. q[++r]=a[i];num[r]=i;
  25. printf("%d ",q[l]);
  26. }
  27. putchar('\n');
  28. l=,r=;
  29. for(int i=;i<k;i++){
  30. for(;l<=r&&i-num[l]>=k;l++);
  31. for(;l<=r&&a[i]>q[r];r--);
  32. q[++r]=a[i];num[r]=i;
  33. }
  34. for(int i=k;i<=n;i++){
  35. for(;l<=r&&i-num[l]>=k;l++);
  36. for(;l<=r&&a[i]>q[r];r--);
  37. q[++r]=a[i];num[r]=i;
  38. printf("%d ",q[l]);
  39. }
  40. return ;
  41. }

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. CodeForces 689E Mike and Geometry Problem (离散化+组合数)

    Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description M ...

  2. [转]directsound抓取麦克风PCM数据封装类

    网上有很多方法从麦克风读取PCM数据,不想一一举例.只是在这里发布一个我自己写的directsound的麦克风PCM数据采集类,通过它,可以很方便的利用directsound技术把麦克风的数据采集到, ...

  3. 备份spfile 中的一个误区

    某书载在备份控制文件的时候,也会自动的备份初始化参数文件,抱着愚钝的 完事亲力亲为的态度,做了如下的小验证. RMAN> list backup of controlfile; specific ...

  4. 内容输出Linux文件系统的的实现:创建一个文件的过程

    题记:写这篇博客要主是加深自己对内容输出的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. 考虑上面这个命令: who > userlist 当这个命令完成后,文件系统增加l一 ...

  5. Webserver推送技术

    server推送(Server Push) 推送技术的基础思想是将浏览器主动查询信息改为server主动发送信息.server发送一批数据,浏览器显示这些数据,同一时候保证与server的连接.当se ...

  6. 几种server模型

    TCP測试用客户程序 每次执行客户程序,在命令行參数指定server的ip地址,port,发起连接的子进程数,和一个待发送的字符串数据,客户程序将模拟多个客户依据指定的子进程数创建子进程来并发的连接到 ...

  7. [AngularJS] ui-router: Abstract States

    ui-router has the powerful ability to define abstract states, or states that can't be navigated to, ...

  8. iOS开发——网络编程Swift篇&(八)SwiftyJSON详解

    SwiftyJSON详解 最近看了一些网络请求的例子,发现Swift在解析JSON数据时特别别扭,总是要写一大堆的downcast(as?)和可选(Optional),看?号都看花了.随后发现了这个库 ...

  9. 【BZOJ1486】【HNOI2009】最小圈 分数规划 dfs判负环。

    链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

  10. phpstorm 和web storm汉化

    http://www.jincaimao.com/cms-phpstorm-index.html phpStorm汉化方法: B1).找到X:\Program Files\JetBrains\PhpS ...