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 ...
随机推荐
- C语言中do...while(0)的妙用
在linux内核代码中,经常看到do...while(0)的宏,do...while(0)有很多作用,下面举出几个: 1.避免goto语句: 通常,如果一个函数开始要分配一些资源,然后如果在中途遇到错 ...
- Delphi管理多线程之线程局部存储:threadvar
尽管多线程能够解决许多问题,但是同时它又给我们带来了很多的问题.其中主要的问题就是:对全局变量或句柄这样的全局资源如何访问?另外,当必须确保一个线程中的某些事件要在另一个线程中的其他时间之前(或之后) ...
- 【stut 逆置正整数】
C语言实验——逆置正整数 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 输入一个三位正整数,将它反向输出. 输入 3位正整数. ...
- 最终排名 sdut 2446
最终排名 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 题目链接:http://acm.sdut.edu.cn/sdutoj/p ...
- windows常用命令
打开"运行"对话框(Win+R),输入cmd,打开控制台命令窗口... 也可以通过cmd /c 命令 和 cmd /k 命令的方式来直接运行命令 注:/c表示执行完命令后关闭cmd ...
- PowerDesigner生成sql及HTML格式数据库文档
一.PowerDesigner生成sql问题 生成sql的方法是 Database -->Generate Database (Ctrl + G ) 但是提示 Could not load VB ...
- ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 349人阅读 评论(0) 收藏
问题一:多线程下获取文件绝对路径 当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办 ...
- [JavaCore] 微信手机浏览器版本判断
公司要做微支付,微信浏览器版本要大于5 package com.garinzhang.web.weixin; import org.apache.commons.lang.StringUtils; i ...
- csipsimple,linphone,webrtc比较
转自: http://www.lxway.com/566299526.htm 最新要做一个移动端视频通话软件,大致看了下现有的开源软件 一) sipdroid1)架构sip协议栈使用JAVA实现,音频 ...
- C# jsonhelper
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Sc ...