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 ;
}

(完)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·~~~~~~~~~~~~~~

最新文章

  1. 3.EasyUI学习总结(三)——easyloader源码分析
  2. springMvc静态资源拦截问题
  3. 虚拟机NAT模式无法上网问题的解决办法
  4. NSKeyedArchive(存储自定义对象)
  5. 小菜的系统框架界面设计-灰姑娘到白雪公主的蜕变(工具条OutLookBar)
  6. jq简单选项卡
  7. JavaScript学习代码整理(一)
  8. Android的移动存储之SharedPreferences
  9. Flask中路由模块的实现
  10. InstallShield安装包卸载-完美卸载
  11. WM_CLOSE、WM_DESTROY、WM_QUIT学习总结(点击关闭按钮会触发WM_CLOSE消息,DestroyWindow API会触发WM_DESTROY和WM_NCDESTROY消息,MSDN上写的很清楚)
  12. js事件循环
  13. spring返回@ResponseBody报406
  14. (七)SpringBoot2.0基础篇- application.properties属性文件的解析及获取
  15. Ubuntu16 搭建Git 服务器
  16. JavaScript 之 BOM
  17. ngnix简介以及如何实现负载均衡原理
  18. oracle补齐日期
  19. VS2017创建一个 ASP.NET Core2.0 应用,并搭建 MVC 框架
  20. POJ 2864

热门文章

  1. c++序列化方法
  2. hdu 1251:统计难题(字典树,经典题)
  3. 《AngularJS》5个实例详解Directive(指令)机制
  4. T-SQL 常用语句
  5. Session 类
  6. 配置ogg异构oracle-mysql(3)目的端配置
  7. 基于ZigBee的家居控制系统的设计与应用
  8. Json转换利器Gson之实例一-简单对象转化和带泛型的List转化 (转)
  9. Linux学习笔记(12)用户和用户组管理
  10. SQLServer 维护脚本分享(06)CPU