POJ 2823 Sliding Window 题解
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
分析:
这道题让我们每次输出区间内的最大值和最小值,如果每次都扫一遍复杂度较高,本题的数据比较大,这种方法时间上无法承受。本题让我们求区间最大最小值,不难想到用线段树解决这个问题,只需要每次用线段树查询区间的最大最小值即可。
核心代码如下:
查询代码:
QAQ Query_Max ( int q , int w , int i )
{
if(q <= tr[i].l && w >= tr[i].r )return tr[i].maxtr ;
else
{
QAQ mid = (tr[i].l + tr[i].r ) >> ;
if(q > mid)
{
return Query_Max ( q , w , i << | );
}
else if(w <= mid)
{
return Query_Max ( q , w , i << );
}
else
{
return Max( Query_Max ( q , w , i << ) , Query_Max ( q , w , i << | ));
}
}
} QAQ Query_Min ( int q , int w , int i )
{
if(q <= tr[i].l && w >= tr[i].r )return tr[i].mintr ;
else
{
QAQ mid = (tr[i].l + tr[i].r ) >> ;
if(q > mid)
{
return Query_Min ( q , w , i << | );
}
else if(w <= mid)
{
return Query_Min ( q , w , i << );
}
else
{
return Min( Query_Min ( q , w , i << ) , Query_Min ( q , w , i << | ));
}
}
}
注:这里QAQ就是long long 用typedef long long QAQ;定义的。
建树及Push_up操作:
void Push_up (int i)
{
tr[i].maxtr = Max ( tr[i << ].maxtr , tr[i << | ].maxtr);
tr[i].mintr = Min ( tr[i << ].mintr , tr[i << | ].mintr);
} void Build_Tree (int x , int y , int i)
{
tr[i].l = x ;
tr[i].r = y ;
if( x == y )tr[i].maxtr = tr[i].mintr = arr[x] ;
else
{
QAQ mid = (tr[i].l + tr[i].r ) >> ;
Build_Tree ( x , mid , i << );
Build_Tree ( mid + , y , i << | );
Push_up ( i );
}
}
以上就是用线段树解法,是线段树的简单应用,本题还有很多其他写法,比如维护单调队列,比线段树更容易实现代码并且代码量较少,以下是维护单调队列的代码:
#include "stdio.h"
#define maxn (1000100)
int n, K;
int Head, Tail;
int val[maxn];
int numb[maxn];
bool Flag;
inline bool cmp(int a, int b)
{
return Flag ? a < b : a > b;
}
void Push(int idx)
{
while(Head < Tail && cmp(val[idx], val[numb[Tail - ]])) Tail --;
numb[Tail++] = idx;
while(Head < Tail && idx - numb[Head] + > K) Head ++;
}
int main()
{
scanf("%d %d", &n, &K);
for(int i = ; i <= n; i++) scanf("%d", &val[i]);
Head = , Tail = , Flag = true;
for(int i = ; i < K; i++) Push(i);
for(int i = K; i <= n; i++)
{
Push(i);
printf("%d ", val[numb[Head]]);
}
puts("");
Head = , Tail = , Flag = false;
for(int i = ; i < K; i++) Push(i);
for(int i = K; i <= n; i++)
{
Push(i);
printf("%d ", val[numb[Head]]);
}
puts("");
return ;
}
(完)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·~~~~~~~~~~~~~~
POJ 2823 Sliding Window 题解的更多相关文章
-
POJ 2823 Sliding Window + 单调队列
一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1) 从队首删除 (2) 从队尾删除 (3) 从队尾插入 (4) ...
-
洛谷P1886 滑动窗口(POJ.2823 Sliding Window)(区间最值)
To 洛谷.1886 滑动窗口 To POJ.2823 Sliding Window 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每 ...
-
题解报告:poj 2823 Sliding Window(单调队列)
Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...
-
poj 2823 Sliding Window (单调队列入门)
/***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...
-
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 ...
-
POJ 2823 Sliding Window(单调队列入门题)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 67218 Accepted: 190 ...
-
POJ 2823 Sliding Window &; Luogu P1886 滑动窗口
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 66613 Accepted: 18914 ...
-
POJ 2823 Sliding Window
Sliding Window Time Limit: 12000MSMemory Limit: 65536K Case Time Limit: 5000MS Description An array ...
-
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 ...
随机推荐
-
第三篇:用SOUI能做什么?
SOUI-DEMO界面预览 在回答SOUI能做什么之前,先看看SVN中demo工程的界面截图: 使用SOUI实现上面的界面主要的工作全在配置几个XML文件,基本不需要写C++代码.(如何配置XML布局 ...
-
VS2010 C++环境下DLL和LIB文件目录及名称修改
VS2010 C++环境下DLL和LIB文件目录及名称修改 转自:http://blog.csdn.net/archielau/article/details/8507581 DLL工程,Debug版 ...
-
走进科学 WAF(Web Appllication Firewall)
1. 前言 当WEB应用越来越为丰富的同时,WEB 服务器以其强大的计算能力.处理性能及蕴含的较高价值逐渐成为主要攻击目标.SQL注入.网页篡改.网页挂马等安全事件,频繁发生. 企业等用户一般采用防火 ...
-
第八章CDC设备
8.1 CDC设备介绍 USB的CDC类是USB通信设备类(Communication Device Class)的简称.CDC类是USB组织定义的一类专门给各种通信设备(电信通信设备和中速网络通信设 ...
-
JVM学习之GC常用算法
出处:博客园左潇龙的技术博客--http://www.cnblogs.com/zuoxiaolong,多谢分享 GC策略解决了哪些问题? 既然是要进行自动GC,那必然会有相应的策略,而这些策略解决了哪 ...
-
MT【256】2016四川高考解答压轴题
(2016四川高考数学解答压轴题)设函数$f(x)=ax^2-a-\ln x,a\in R$. 1)讨论$f(x)$的单调性;2)确定$a$的所有可能值,使得$f(x)>\dfrac{1}{x} ...
-
Tomcat问题:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined ,At least one of these environment variable is needed to run this program
一眼就能看出来是jdk的环境有问题,但是用了这么久的jdk一直都配置的好好的,怎么一到Tomcat上就这么矫情了. 最后查解决方案,原来是我的jdk从官网直接下载的,虽然我修改了java_home,但 ...
-
Android上实现视频录制
首先,我们肯定要用到摄像头,因此需要在Manifest文件中声明使用权限: <uses-permission android:name="android.permission.CAME ...
-
[Phonegap+Sencha Touch] 移动开发29 安卓navigator.camera.getPicture得到图片的真实路径
原文地址:http://blog.csdn.net/lovelyelfpop/article/details/38313021 phonegap的拍照插件选择图库中的图片.代码例如以下: naviga ...
-
[mysql] 查询前几条记录
From: http://www.cnblogs.com/xuxm2007/archive/2010/11/16/1878211.html SELECT * FROM table LI ...