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

  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

Source

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

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<map>
  5. using namespace std;
  6. const int mxn=;
  7. int n,k;
  8. int num;
  9. int qmx[mxn],qmi[mxn];
  10. int lmx[mxn],lmi[mxn];
  11. int ansmx[mxn];
  12. int main(){
  13. scanf("%d%d",&n,&k);
  14. int i,j;
  15.  
  16. int h1=,t1=;
  17. int h2=,t2=;
  18. for(int cnt=;cnt<=n;cnt++){
  19. scanf("%d",&num);
  20. //max
  21. while(h1<=t1 && num>=qmx[t1])t1--;
  22. qmx[++t1]=num;
  23. lmx[t1]=cnt;//下标
  24.  
  25. //min
  26. while(h2<=t2 && num<=qmi[t2])t2--;
  27. qmi[++t2]=num;
  28. lmi[t2]=cnt;
  29.  
  30. if(lmx[t1]-lmx[h1]>=k)h1++;
  31. if(lmi[t2]-lmi[h2]>=k)h2++;
  32. // printf("test: h1:%d t1:%d\n",h1,t1);
  33. ansmx[cnt]=qmx[h1];
  34. if(cnt>=k)printf("%d ",qmi[h2]);
  35. }
  36. printf("\n");
  37. for(i=k;i<=n;i++)printf("%d ",ansmx[i]);
  38.  
  39. return ;
  40. }

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. JMeter学习(三十二)属性和变量

    一.Jmeter中的属性: 1.JMeter属性统一定义在jmeter.properties文件中,我们可以在该文件中添加自定义的属性 2.JMeter属性在测试脚本的任何地方都是可见的(全局),通常 ...

  2. java11-2 String面试题

    package cn.itcast_02; /* * String s = new String(“hello”)和String s = “hello”;的区别? * 有.前者会创建2个对象,后者创建 ...

  3. lcx转发 【解决内网没法链接3389 的问题】

    要求我自己在外网 然后监听1111端口,将1111端口数据流量转发至2222端口 被入侵主机上 将本地的2222端[愿3389 主机修改了远程连接的端口]口流量转发至外网ip的1111端口 2222为 ...

  4. Android 属性动画(Property Animation) 完全解析 (上)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38067475 1.概述 Android提 供了几种动画类型:View Anima ...

  5. PHP安装memcache扩展接口步骤

    1.将php_memcache.dll文件保存到php的应用程序扩展ext目录中 2.在php.ini配置文件添加扩展的位置,加入一行extension=php_memcache.dll 3.重新启动 ...

  6. CardboardCamera Prefab 中文笔记

    在Cardboard的预制体(Prefab)中, CardboardCamera是最简单的一个,仅有两个子物体,一个PostRender, 一个PreRender,以及分别带的Camera组件. Ca ...

  7. Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

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

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

  9. 处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表

    IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决办法 IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Int ...

  10. 动画:UIKitAnimation 简单动画学习 iOS (一) 渐变 、 移动 、翻页、来回翻转 ——转载

    转载请说明(谢谢) http://blog.csdn.net/a21064346/article/details/7851695 点击打开链接 以下 一个系列的 动画效果 在 UIView.h文件中可 ...