Sliding Window

Time Limit: 12000MS
Memory Limit: 65536K

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
 
   1: #include <iostream>

   2: #include <cstdio>

   3: #include <cstring>

   4: #include <algorithm>

   5: using namespace std;

   6: #define lson l,m,rt<<1

   7: #define rson m+1,r,rt<<1|1

   8: typedef long long ll;

   9: const int maxn=1111111;

  10: int a[maxn<<2],b[maxn<<2];

  11: int small[maxn],big[maxn],cnt=0;

  12:  

  13: void PushUp(int rt)

  14: {

  15:     a[rt]=max(a[rt<<1],a[rt<<1|1]);

  16:     b[rt]=min(b[rt<<1],b[rt<<1|1]);

  17: }

  18:  

  19: void build(int l,int r,int rt)

  20: {

  21:     if(l==r){

  22:         scanf("%d",&a[rt]);

  23:         b[rt]=a[rt];

  24:         return;

  25:     }

  26:     int m=(l+r)>>1;

  27:     build(lson); build(rson);

  28:     PushUp(rt);

  29: }

  30:  

  31: void query(int L,int R,int l,int r,int rt,int &ma,int &mi)

  32: {

  33:     if(L<=l&&R>=r)

  34:     {

  35:         ma=a[rt];

  36:         mi=b[rt];

  37:         return;

  38:     }

  39:     int m=(r+l)>>1;

  40:     if(L<=m&&R>m)

  41:     {

  42:         int x1,y1,x2,y2;

  43:         query(L,R,lson,x1,y1);

  44:         query(L,R,rson,x2,y2);

  45:         ma=max(x1,x2);

  46:         mi=min(y1,y2);

  47:     }

  48:     else if(L<=m)

  49:         query(L,R,lson,ma,mi);

  50:     else

  51:         query(L,R,rson,ma,mi);

  52: }

  53:  

  54: void show(int n,int e[])

  55: {

  56:     for(int i=0; i<n ; i++){

  57:         if(i) printf(" ");

  58:         printf("%d",e[i]);

  59:     }

  60:     printf("\n");

  61: }

  62:  

  63: void run(int n,int k)

  64: {

  65:     cnt=0;

  66:     build(1,n,1);

  67:     for(int i=k; i<=n; i++)

  68:     {

  69:         query(i-k+1,i,1,n,1,big[cnt],small[cnt]);

  70:         cnt++;

  71:     }

  72:     show(cnt,small);

  73:     show(cnt,big);

  74: }

  75:  

  76: int main()

  77: {

  78:     int n,k;

  79:     while(scanf("%d%d",&n,&k)!=EOF)

  80:         run(n,k);

  81:     return 0;

  82: }

 

POJ 2823 Sliding Window的更多相关文章

  1. POJ 2823 Sliding Window + 单调队列

    一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1)   从队首删除 (2)   从队尾删除 (3)   从队尾插入 (4)   ...

  2. POJ 2823 Sliding Window 题解

    POJ 2823 Sliding  Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...

  3. 洛谷P1886 滑动窗口(POJ.2823 Sliding Window)(区间最值)

    To 洛谷.1886 滑动窗口 To POJ.2823 Sliding Window 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每 ...

  4. poj 2823 Sliding Window (单调队列入门)

    /***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...

  5. POJ 2823 Sliding Window ST RMQ

    Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...

  6. POJ 2823 Sliding Window(单调队列入门题)

      Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 67218   Accepted: 190 ...

  7. POJ 2823 Sliding Window & Luogu P1886 滑动窗口

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 66613   Accepted: 18914 ...

  8. POJ - 2823 Sliding Window (滑动窗口入门)

    An array of size n ≤ 10 6 is given to you. There is a sliding window of size kwhich is moving from t ...

  9. POJ 2823 Sliding Window (滑动窗口的最值问题 )

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 41264   Accepted: 12229 ...

随机推荐

  1. P6 EPPM Manual Installation Guide (Oracle Database)

    P6 EPPM Manual Installation Guide (Oracle Database) P6 EPPM Manual Installation Guide (Oracle Databa ...

  2. C#操作IIS程序池及站点的创建配置

    最近在做一个WEB程序的安装包:对一些操作IIS进行一个简单的总结:主要包括对IIS进行站点的新建以及新建站点的NET版本的选择,还有针对IIS7程序池的托管模式以及版本的操作:首先要对Microso ...

  3. 设计模式--原型(Prototype)模式

    写这些也许有人认为“为了模式而模式”.Insus.NET所想到的,每个大师成为大师之前,也许都得这样做. 走路,从小就开始学,直至现在,谁还不是为了走路而走路?一直重复着...... 很多人没有分享自 ...

  4. C# 实现WinForm窗口最小化到系统托盘代码,并且判断左右鼠标的事件

    1.设置WinForm窗体属性showinTask=false 2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标. 3.添加窗体最小 ...

  5. sql: sq_helptext

    --查看表生成脚本 sql server     --- '\r'是回车,'\n'是换行  /t相当于键盘的Tab键    --- 操作系统的不同,换行符操也不同:/r Mac /n Unix/Lin ...

  6. 无法打开Android SDK Manager的解决办法

    不知道从什么时候开始,打开Android的SDK Manager.exe时,命令行窗口一闪就自动关掉了. 想更新一些Android的东西都更新不了. 查了一下,解决办法是: 环境变量的系统变量Path ...

  7. Spring框架之AOP

    SpringAop: 1.加入 jar 包 com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver ...

  8. 【OpenCV & CUDA】OpenCV和Cuda结合编程

    一.利用OpenCV中提供的GPU模块 目前,OpenCV中已提供了许多GPU函数,直接使用OpenCV提供的GPU模块,可以完成大部分图像处理的加速操作. 基本使用方法,请参考:http://www ...

  9. .NET Core创建一个控制台(Console)程序

    .NET Core版本:1.0.0-rc2 Visual Studio版本:Microsoft Visual Studio Community 2015 Update 2 开发及运行平台:Window ...

  10. API的非向后兼容性无论如何通常代表着一种比较差的设计

    不管一个类库或者工具方法实现多么的好,如果无法做到向后兼容性,通常会给用户带来很大的升级成本,很多对此的依赖如果希望在后续的升级和维护期间使用该类库的其他新增特性或者好处,将不得不推迟升级亦或是被迫接 ...