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 ...
随机推荐
- Android优化——UI优化(五) Listview 重用convertView
1.重用convertView 我们对convertView添加判断,如果存在我们就直接使用,否则初始化一个convertView的实例.(如下图) 2.使用viewHolder 使用viewHold ...
- ClickJacking(点击劫持)
问题: 点击劫持(ClickJacking)是一种视觉上的欺骗手段.大概有两种方式,一是攻击者使用一个透明的iframe,覆盖在一个网页上,然后诱使用户在该页面上进行操作,此时用户将在不知情的情况下点 ...
- 使用CSS3画出一个叮当猫
刚学习了这个案例,然后觉得比较好玩,就练习了一下.然后发现其实也不难,如果你经常使用PS或者Flash的话,应该就会知道画个叮当猫是很容易的事,至少我是这么觉得.但是,用CSS3画出来确实是第一次接触 ...
- iOS下使用SHA1WithRSA算法加签源码
首先了解一下几个相关概念,以方便后面遇到的问题的解决: RSA算法:1977年由Ron Rivest.Adi Shamirh和LenAdleman发明的,RSA就是取自他们三个人的名字.算法基于一个数 ...
- LUA 协程
LUA协程和C#协程非常相似,功能与用法更强大.基础用法: coco = coroutine.create(function (a,b) print("resume args:". ...
- 利用mstsc远程桌面传送文件,记录一下
尼玛之前服务器上传有点异常,在服务器装了一个上传下载监控的软件,用的是什么 绿色版QQ流量监控,绿色是挺绿色的,装了就等哭吧.没时间打字反正就是删除不了,后来想办法删除了.艹.所有服务除了系统服务能上 ...
- VS一般设置(字体,背景色)
字体 打开工具=>环境=>字体和颜色,字体:Consolas,大小:13 背景色 缩进设置 工具=>文本编辑器=>纯文本=>制表符=>保留制表符
- Mtk Ft6306 touch 驱动 .
1.1. MTK Touch 驱动的组成Mtk Touch driver 驱动包括:Mtk platform 虚拟平台设备驱动.Module touch IC 驱动.Input subsys ...
- 简单统计SQLSERVER用户数据表大小(包括记录总数和空间占用情况)
在SQLSERVER,简单的组合sp_spaceused和sp_MSforeachtable这两个存储过程,可以方便的统计出用户数据表的大小,包括记录总数和空间占用情况,非常实用,在SqlServer ...
- mongodb与sql语句对照表
inert into users value(3,5) db.users.insert({a:3,b:5}) select a,b from users db.users.find({}, { ...