POJ 2823 Sliding Window 题解
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 ...
随机推荐
- 实现VS2010整合NUnit进行单元测试(转载)
代码编写,单元测试必不可少,简单谈谈Nunit进行单元测试的使用方式: 1.下载安装NUnit(最新win版本为NUnit-2.6.4.msi) http://www.nunit.org/index. ...
- java中常用的工具类(一)
我们java程序员在开发项目的是常常会用到一些工具类.今天我汇总了一下java中常用的工具方法.大家可以在项目中使用.可以收藏!加入IT江湖官方群:383126909 我们一起成长 一.String工 ...
- Install Docker on Mac OS X(转)
Install Docker on Mac OS X You can install Docker using Boot2Docker to run docker commands at your c ...
- jQuery插件treeview点击节点名称不展开、收缩节点 分类: JavaScript 2014-06-16 20:28 539人阅读 评论(0) 收藏
修改jquery.treeview.js文件中的applyClasses方法(注释掉两行代码): 修改后的applyClasses方法如下: applyClasses: function(settin ...
- Oracle优化-表设计
前言 绝大多数的Oracle数据库性能问题都是由于数据库设计不合理造成的,只有少部分问题根植于Database Buffer.Share Pool.Redo Log Buffer等内存模块配置不合理, ...
- [Eclipse] Eclipse配置Tomcat插件
1 . Eclipse IDE 3.6 for Java EE Developersat- 5.5.28 或者以上版本 : 2 . 安装 Tomcat 插件 , 文件名: tomcatPluginV3 ...
- 设置SecureCRT会话的缓冲区大小
转自:http://blog.csdn.net/imxiangzi/article/details/7457703 在使用SecureCRT操作设备时,默认的回滚行数为500行.可以通过打开[选项]- ...
- php随机生成验证码
我们经常需要服务器向前端发送验证码,验证码需要随机产生,下面的用简单的代码实现了这一过程: <?php $pool='0123456789abcdefghijklmnopqrstuvwxyzAB ...
- Hbuilder连接模拟器调试
Hbuilder是一个非常好用的HTML5开发IDE,我最喜欢的功能就是连接手机调试了,连接手机调试有两种途径,一是通过USB连接真机,二是下载安装一个安卓模拟器,让Hbuilder连接到安卓模拟器, ...
- Android在listview添加checkbox实现单选多选操作问题(转)
转自:http://yangshen998.iteye.com/blog/1310183 在Android某些开发需求当中,有时候需要在listveiw中加入checkbox实现单选,多选操作.表面上 ...