poj 3264 区间最大最小值 RMQ问题之Sparse_Table算法
Balanced Lineup
Time Limit: 5000 MS Memory Limit: 0 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
Description
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
Input
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.
Output
integer that is a response to a reply and indicates the difference in
height between the tallest and shortest cow in the range.
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
///RMQ问题之Sparse_Table算法 模板 区间最大最小值 #include <math.h>
#include <stdio.h>
#include <iostream> #define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b using namespace std;
const int N=;
int n,Q,c[N],a,b;
int dp_max[N][]; ///33足够用 2^32 是 int
int dp_min[N][]; void Init()
{
for(int i=; i<=n; i++)
dp_max[i][] = dp_min[i][] = c[i];
double limit = log(n)/log(2.0);
for(int j=; j<=(int)limit; j++)
for(int i=; i+(<<j)-<=n; i++)
{
dp_max[i][j] = max(dp_max[i][j-],dp_max[i+(<<(j-))][j-]);
dp_min[i][j] = min(dp_min[i][j-],dp_min[i+(<<(j-))][j-]);
}
}
int Get_Max(int a,int b)
{
int k = (int)(log(b-a+)/log(2.0));
return max(dp_max[a][k],dp_max[b-(<<k)+][k]);
}
int Get_Min(int a,int b)
{
int k = (int)(log(b-a+)/log(2.0));
return min(dp_min[a][k],dp_min[b-(<<k)+][k]);
}
int main()
{
scanf("%d%d",&n,&Q);
for(int i=; i<=n; i++)
scanf("%d",&c[i]);
Init();
while(Q--)
{
scanf("%d%d",&a,&b);
printf("%d\n",Get_Max(a,b)-Get_Min(a,b));
}
return ;
}
RMQ问题,全名(Range Minimum/Maximum Query),是求给定区间中的最值问题。
主要方法及复杂度如下:
1、朴素(即搜索),O(n)-O(qn) online。
2、线段树,O(n)-O(qlogn) online。
3、Sparse_Table(实质是动态规划),O(nlogn)-O(1) online。
4、RMQ标准算法:先规约成LCA(Lowest Common Ancestor),再规约成约束RMQ,O(n)-O(1) online。
ST算法可以在O(nlogn)的预处理以后实现O(1)的查询效率,从而解决查询次数很多(如大于100万)的RMQ问题。
首先,是预处理。预处理是采用dp的思想,我们用f[i][j]表示区间[i,i+2^j-1]中的最大值(即从i开始,长度为2^j的闭区间)。
开始时,f[i][0]一定等于num[i]。好了,初始值找到了,下面是状态转移方程:
f[i][j]=max(f[i][j-1],f[i+2^(j-1)][j-1])。即把[i,i+2^j-1]区间分成两部分[i,i+2^(j-1)-1]和[i+2^(j-1),i+2^(j-1)+2^(j-1)-1],正好和原区间一致。
有了初始值和转移方程,我们可以自底向上递推出所有的f[i][j]的值。
对于边界,还要注意一点。由于区间长度最大为n,所以二维边界最大为log(n)/log(2.0);
一维边界只要满足对于每个起始点,都可以有长度找到n就行了,也就是让i+2^j-1<=n就好了。
然后就是查询了。假设要查询区间[a,b]的最大值,由于区间的长度很可能不是2的整数幂,所以我们要把区间划分为长度为2的整数幂的两部分,而且
这两个区间的并集必须是[a,b]。为了实现这个方案,我们需要先求出一个最大的k,使得2^k<=(b-a+1),这样就可以把区间分成两部分
[a,a+2^k-1]和[b-2^k+1,b],使他们既能不超过a,b区间的范围,又能把区间全部覆盖。于是,[a,b]区间的最大值就等于上述两个
区间的最大值中最大的那个。(有点绕)
poj 3264 区间最大最小值 RMQ问题之Sparse_Table算法的更多相关文章
- POJ 3264 区间最大最小值Sparse_Table算法
题目链接:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total ...
- POJ 3264 Balanced Lineup(RMQ)
点我看题目 题意 :N头奶牛,Q次询问,然后给你每一头奶牛的身高,每一次询问都给你两个数,x y,代表着从x位置上的奶牛到y位置上的奶牛身高最高的和最矮的相差多少. 思路 : 刚好符合RMQ的那个求区 ...
- 【RMQ】【Sparse_Table算法】
摘自网友,具体哪个忘记了,抱歉~ 定义: RMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题: 对于长度为n的数列A,回答若干询问RMQ(A,i,j) ...
- POJ 3264 Balanced Lineup 简单RMQ
题目:http://poj.org/problem?id=3264 给定一段区间,求其中最大值与最小值的差. #include <stdio.h> #include <algorit ...
- (简单) POJ 3264 Balanced Lineup,RMQ。
Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...
- POJ 3171 区间覆盖最小值&&线段树优化dp
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4715 Accepted: 1590 D ...
- poj 3264 Balanced Lineup(RMQ裸题)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 43168 Accepted: 20276 ...
- poj 3264 Balanced Lineup【RMQ-ST查询区间最大最小值之差 +模板应用】
题目地址:http://poj.org/problem?id=3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0分析:标 ...
- POJ - 3264 线段树模板题 询问区间最大最小值
这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...
随机推荐
- bowtie:短序列比对的新工具
bowtie:短序列比对的新工具(转) (2014-11-17 22:15:24) 转载▼ 标签: 转载 原文地址:bowtie:短序列比对的新工具(转)作者:玉琪星兆 Bowtie是一个超级快速 ...
- Linux命令大全完整版
1. linux系统管理命令 adduser 功能说明:新增用户帐号.语 法:adduser补充说明:在Slackware中,adduser指令是个script程序,利用交谈的方式取得输入的用户帐 ...
- Windows 修改的hosts记录没有效果
windows修改的hosts记录没有效果,新添加的也没有效果. 检查DNS设置相关的均正常, <Dns client为此计算机解析和缓冲域名系统 (DNS) 名称.> 为此计算机注册并更 ...
- Delphi XE2有什么新功能
具体内容见PDF Delphi XE2有什么新功能Delphi XE2提供(offers)了令人兴奋(exciting)的新功能,让您能够建立高度可视化的,在Windows,Mac和iOS上的业务应用 ...
- windows driver
C:\Windows\System32\DriverStore\FileRepository
- Spring 系列教程之自定义标签的解析
Spring 系列教程之自定义标签的解析 在之前的章节中,我们提到了在 Spring 中存在默认标签与自定义标签两种,而在上一章节中我们分析了 Spring 中对默认标签的解析过程,相信大家一定已经有 ...
- Python之路(第二十篇) subprocess模块
一.subprocess模块 subprocess英文意思:子进程 那什么是进程呢? (一)关于进程的相关理论基础知识 进程是对正在运行程序的一个抽象,进程的概念起源于操作系统,是操作系统最核心的概念 ...
- mysql索引相关
索引有主键索引.唯一索引.普通索引 单列索引,复合索引. 复合索引(a,b,c),可以理解是有三个索引,分别是a.b.c三个索引 前缀不是a的话,复合索引都不起作用,前缀用函数或者是范围,比如< ...
- mysql cmd 无法登录
第一次折腾mysql诉苦记 版本注明: mysql 5.7.21 本地部署mysql,配置完成后(配置没有问题) cmd命令连接mysql: mysql -uroot -p 提示: ERROR 104 ...
- Java环境编写
首先安装jdk,本系统中jdk安装在D:\jdk:jre安装在D:\Jre: 然后开始配置环境变量: JAVA_HOME:D:\jdk; JRE_HOME:D:\jre; CLASSPATH:.;%J ...