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 ...
随机推荐
- wifi基础知识整理
转自 :http://blog.chinaunix.net/uid-9525959-id-3326047.html WIFI基本知识整理 这里对wifi的802.11协议中比较常见的知识做一个基本的总 ...
- Delphi的Win32的API调用简单介绍
1. 介绍Win32 API和Win32系统.还要讨论Win32系统的功能以及它与16位系统在功能上的几个主要区别.只是让对Win32系统有一个基本的了解.当已经基本了解Win32操作后,就可 ...
- 关于Python 获取windows信息收集
收集一些Python操作windows的代码 (不管是自带的or第三方库)均来自网上 1.shutdown 操作 定时关机.重启.注销 #!/usr/bin/python #-*-coding:utf ...
- MVC中Form表单的提交
概述 Web页面进行Form表单提交是数据提交的一种,在MVC中Form表单提交到服务器.服务端接受Form表单的方式有多种,如果一个Form有2个submit按钮,那后台如何判断是哪个按钮提交的数据 ...
- ArcGIS图层和要素的过滤显示
ArcGIS可以设置动态地图服务(ArcGISDynamicMapServiceLayer)显示哪些图层,也可以设置每个图层根据某个属性字段的某些条件来进行过滤显示. 1.设置显示的图层 主要是通过A ...
- ereg/eregi报错处理办法
ereg()函数和eregi()函数用法相同,不同之处在与ereg()区分大小写,eregi()不区分大小写 在php5.3以上的版本将不再支持eregi()和ereg()函数 处理办法: 正则函数处 ...
- Bootstrap看厌了?试试Metro UI CSS吧
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:Bootstrap作为一款超级流行的前端框架,已经成为很多人的首选,不过有时未免有点审 ...
- Android LayoutInflater详解(转)
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- Vs2010工具栏显示“开始执行“按钮
转载来源:http://blog.csdn.net/fromhj/article/details/8795047 前言 在用visual studio 2010的时候,要运行程序,可以使用 1.菜单- ...
- centos7 卸载mysql
[root@zyf ~]# rpm -qa|grep -i mysql mysql-community-libs--.el7.x86_64 mysql-community-server--.el7.x ...