POJ2823 Sliding Window
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 53086 | Accepted: 15227 | |
Case Time Limit: 5000MS |
Description
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
Output
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
Source
模拟滑动区间,用单调队列维护最值即可。本来正常做法是用单调队列存下标,然而蠢蠢的我又多开了俩数组……这样效率会降低,好在还是过了。
- #include<cstdio>
- #include<iostream>
- #include<algorithm>
- #include<map>
- using namespace std;
- const int mxn=;
- int n,k;
- int num;
- int qmx[mxn],qmi[mxn];
- int lmx[mxn],lmi[mxn];
- int ansmx[mxn];
- int main(){
- scanf("%d%d",&n,&k);
- int i,j;
- int h1=,t1=;
- int h2=,t2=;
- for(int cnt=;cnt<=n;cnt++){
- scanf("%d",&num);
- //max
- while(h1<=t1 && num>=qmx[t1])t1--;
- qmx[++t1]=num;
- lmx[t1]=cnt;//下标
- //min
- while(h2<=t2 && num<=qmi[t2])t2--;
- qmi[++t2]=num;
- lmi[t2]=cnt;
- if(lmx[t1]-lmx[h1]>=k)h1++;
- if(lmi[t2]-lmi[h2]>=k)h2++;
- // printf("test: h1:%d t1:%d\n",h1,t1);
- ansmx[cnt]=qmx[h1];
- if(cnt>=k)printf("%d ",qmi[h2]);
- }
- printf("\n");
- for(i=k;i<=n;i++)printf("%d ",ansmx[i]);
- return ;
- }
POJ2823 Sliding Window的更多相关文章
- POJ2823 Sliding Window (单调队列)
POJ2823 Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 38342 Accepte ...
- codevs4373 窗口==poj2823 Sliding Window
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 53676 Accepted: 15399 ...
- POJ2823 Sliding Window(单调队列)
单调队列,我用deque维护.这道题不难写,我第二次写单调队列,1次AC. -------------------------------------------------------------- ...
- POJ2823 Sliding Window(单调队列)
题目要输出一个序列各个长度k的连续子序列的最大值最小值. 多次RMQ的算法也是能过的,不过单调队列O(n). 这题,队列存元素值以及元素下标,队尾出队维护单调性然后入队,队首出队保持新元素下标与队首元 ...
- [POJ2823]Sliding Window 滑动窗口(单调队列)
题意 刚学单调队列的时候做过 现在重新做一次 一个很经典的题目 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗 ...
- poj2823 Sliding Window luogu1886 滑动窗口 单调队列
模板题 #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...
- POJ2823 Sliding Window【双端队列】
求连续的k个中最大最小值,k是滑动的,每次滑动一个 用双端队列维护可能的答案值 假设要求最小值,则维护一个单调递增的序列 对一開始的前k个,新增加的假设比队尾的小.则弹出队尾的,直到新增加的比队尾大. ...
- [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 ...
- LeetCode题解-----Sliding Window Maximum
题目描述: Given an array nums, there is a sliding window of size k which is moving from the very left of ...
随机推荐
- JMeter学习(三十二)属性和变量
一.Jmeter中的属性: 1.JMeter属性统一定义在jmeter.properties文件中,我们可以在该文件中添加自定义的属性 2.JMeter属性在测试脚本的任何地方都是可见的(全局),通常 ...
- java11-2 String面试题
package cn.itcast_02; /* * String s = new String(“hello”)和String s = “hello”;的区别? * 有.前者会创建2个对象,后者创建 ...
- lcx转发 【解决内网没法链接3389 的问题】
要求我自己在外网 然后监听1111端口,将1111端口数据流量转发至2222端口 被入侵主机上 将本地的2222端[愿3389 主机修改了远程连接的端口]口流量转发至外网ip的1111端口 2222为 ...
- Android 属性动画(Property Animation) 完全解析 (上)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38067475 1.概述 Android提 供了几种动画类型:View Anima ...
- PHP安装memcache扩展接口步骤
1.将php_memcache.dll文件保存到php的应用程序扩展ext目录中 2.在php.ini配置文件添加扩展的位置,加入一行extension=php_memcache.dll 3.重新启动 ...
- CardboardCamera Prefab 中文笔记
在Cardboard的预制体(Prefab)中, CardboardCamera是最简单的一个,仅有两个子物体,一个PostRender, 一个PreRender,以及分别带的Camera组件. Ca ...
- Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- iBatis.Net(C#)数据库查询
引用请注明http://www.cnblogs.com/13590/archive/2013/03/14/2958735.html 摘要:查询是数据库SQL语言的核心,本文介绍了通过iBatis.N ...
- 处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表
IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决办法 IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Int ...
- 动画:UIKitAnimation 简单动画学习 iOS (一) 渐变 、 移动 、翻页、来回翻转 ——转载
转载请说明(谢谢) http://blog.csdn.net/a21064346/article/details/7851695 点击打开链接 以下 一个系列的 动画效果 在 UIView.h文件中可 ...