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手动安装指南(Oracle数据库)(一)

    P6 EPPM手动安装指南(Oracle数据库) P6 EPPM Manual Installation Guide (Oracle Database) 1.      内容... 1 1.1.    ...

  2. Winform开发框架之插件化应用框架实现

    支持插件化应用的开发框架能给程序带来无穷的生命力,也是目前很多系统.程序追求的重要方向之一,插件化的模块,在遵循一定的接口标准的基础上,可以实现快速集成,也就是所谓的热插拔操作,可以无限对已经开发好系 ...

  3. AEAI DP开发平台升级说明

    本次发版的AEAI DP_v3.5.0版本为AEAI DP _v3.4.0版本的升级版本,该产品现已开源并上传至开源社区http://www.oschina.net/p/aeaidp. 1 升级说明 ...

  4. 数据库一次性插入10w条数据,怎么插入效率快

    在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题 下面介绍SQL Server支持的两种批量 ...

  5. 【C#进阶系列】07 常量和字段

    常量 常量总是被视为静态成员. 常量其实可以不限于基元类型,但是必须初始化为null.(我觉得这个点知道和不知道都一样,我已经自动从脑海中忽略了.很多时候在我这个人眼中,艰涩的代码和垃圾代码,其实没有 ...

  6. 【jQuery基础学习】08 编写自定义jQuery插件

    目的:虽然jQuery各种各样的功能已经很完善了,但是我们还是要学会自己去编写插件.这样我们可以去封装一些项目中经常用到的专属的代码,以便后期维护和提高开发效率. jQuery插件的类型: 封装对象方 ...

  7. 重新想象 Windows 8 Store Apps (71) - 其它: C# 调用 C++

    [源码下载] 重新想象 Windows 8 Store Apps (71) - 其它: C# 调用 C++ 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 其它 C# ...

  8. 当使用母版页时JavaScript客户端获取服务器控件的Id

    当使用MasterPage.UserControl等容器时,为了避免控件的重复命名,asp.net会自动将容器中的控件生成一个ClientID(Control Tree中的可生成,否则不会生成). J ...

  9. 百度地图API自定义地图

    http://api.map.baidu.com/lbsapi/creatmap/index.html http://developer.baidu.com/map/index.php?title=w ...

  10. JavaScript 中有关数组对象的方法

    JS 处理数组多种方法 js 中的数据类型分为两大类:原始类型和对象类型. 原始类型包括:数值.字符串.布尔值.null.undefined 对象类型包括:对象即是属性的集合,当然这里又两个特殊的对象 ...