RMQ算法

简单来说,RMQ算法是给定一组数据,求取区间[l,r]内的最大或最小值。

例如一组任意数据 5 6 8 1 3 11 45 78 59 66 4,求取区间(1,8)  内的最大值。数据量小时,只需遍历一遍就可以,数据量一大时就容易时间超限,RMQ算法是一种高效算法,和线段树差不多(当没有数据的实时更新时),当然两者都需要预处理。

定义映射f(i,j)=x,即以i为起点,长度为2j 区间内的最大最小值,显而易见f(i,0)为该数本身,那么求f(i,j+1)时;

可得公式 f(i,j)=max(f(i,j-1),f(i+(1<<j-1),j-1) 即f(1,2)=max(f(1,0),f(2,0))=6;

代码如下:

void get_RMQ()
{
for(int i=;i<=n;i++)
{
scanf("%d",&x);
maxx[i][]=x;
minx[i][]=x;
}
for(int j=;(<<j)<=n;j++)
{
for(int i=;i+(<<j)<=n;j++)
{
maxx[i][j]=max(maxx[i][j-],maxx[i+(<<(j-))][j-]);
minx[i][j]=min(minx[i][j-],minx[i+(<<(j-))][j-]);
}
}
}

例题 UVA 11235 Frequent Values(RMQ)

You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among the integers ai , ... , aj.

Input Specification

The input consists of several test cases. Each test case starts with a line containing two integers n and q(1 ≤ n, q ≤ 100000). The next line contains n integers a1 , ... , an (-100000 ≤ ai ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces. You can assume that for each i ∈ {1, ..., n-1}: ai ≤ ai+1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the query.

The last test case is followed by a line containing a single 0.

Output Specification

For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.

Sample Input

10 3
-1 -1 1 1 1 1 3 10 10 10
2 3
1 10
5 10
0

Sample Output

1
4
3
题目大意,给定一个非降序数列,求给定区间内出现的某个数出现的最大重复次数。
RMQ思路:
因为非降序,所以-1记为第一个数,出现了两次,然后lx[1]记录原数组内-1出现的首位置,rx[1]记录原数组内-1最后出现的位置ans[i]=tot,即ans[1]=ans[2]=1,
则原数组转化为2,4,1,3;查询l,r时先判断ans[l]是否等于ans[r] 若ans[l]==ans[r] 则说明 区间(l,r)内都是同一个数,所以结果为r-l+1;
否则 result=max(RMQ(ans[l]+1,ans[r]-1),max(r-lx[ans[r]]+1,rx[ans[l]]-l+1);因为断点处无法准确判断所以单独考虑。
RMQ代码如下 时间:540ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int mod=1e5+;
int ans[mod],pos[mod],lx[mod],rx[mod];
int dp[mod][],tot,x,n,q,l,r,last;
int max(int x,int y){
return x>y?x:y;
}
void get_RMQ()//dp预处理
{
for(int i=;i<=tot;i++){
dp[i][]=pos[i];
}
for(int j=;(<<j)<=tot;j++)
{
for(int i=;i+(<<j)-<=tot;i++)
{
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
}
}
}
int RMQ(int l,int r)//查询
{
if(l>r) return ;
int k=;
while(<<(+k)<=r-l+)k++;
return max(dp[l][k],dp[r-(<<k)+][k]);
}
int main()
{
while(~scanf("%d",&n)&&n)
{
scanf("%d",&q);
tot=;
memset(dp,,sizeof(dp));
memset(ans,,sizeof(ans));
memset(pos,,sizeof(pos));
memset(lx,,sizeof(lx));
memset(rx,,sizeof(rx));
for(int i=;i<=n;i++)
{
scanf("%d",&x);
if(i==){last=x;++tot;lx[tot]=;}
if(x==last){ans[i]=tot;pos[tot]++;rx[tot]++;}
else {ans[i]=++tot;pos[tot]++;lx[tot]=rx[tot]=i;last=x;}
}
get_RMQ();
while(q--)
{
scanf("%d%d",&l,&r);
if(ans[l]==ans[r]) printf("%d\n",r-l+);
else printf("%d\n",max(RMQ(ans[l]+,ans[r]-),max(rx[ans[l]]-l+,r-lx[ans[r]]+)));
//端点处单独考虑
}
}
return ;
}

再来一个线段树代码,时间:730ms

//线段树查询
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int mod=1e5+;
int node[mod*],ans[mod],pos[mod],lx[mod],rx[mod];
int last,x,n,q,r,l,tot,cont;
int max(int x,int y){
return x>y?x:y;
}
void PushUp(int t){
node[t]=max(node[t<<],node[(t<<)+]);
}
void build(int l,int r,int t){//建造线段树
int mid;
mid=(l+r)>>;
if(l==r){
node[t]=pos[++cont];//注意数据的导入,并不是与t相等
return;
}
build(l,mid,t<<);
build(mid+,r,(t<<)+);
PushUp(t);
}
int query(int ll,int rr,int l,int r,int t){//区间查询
int k=;
int mid=(l+r)>>;
if(ll<=l && rr>=r) return node[t];
if(ll<=mid) k=max(k,query(ll,rr,l,mid,t<<));
if(rr>mid) k=max(k,query(ll,rr,mid+,r,(t<<)+));
return k;
}
int main(){
while(~scanf("%d",&n)&&n){
scanf("%d",&q);
tot=;
memset(ans,,sizeof(ans));
memset(lx,,sizeof(lx));
memset(rx,,sizeof(rx));
memset(pos,,sizeof(pos));
for(int i=;i<=n;i++){
scanf("%d",&x);
if(i==){last=x;++tot;lx[tot]=;}
if(x==last){ans[i]=tot;pos[tot]++;rx[tot]++;}//数据的整合
else {ans[i]=++tot;pos[tot]++;lx[tot]=rx[tot]=i;last=x;}
}
cont=;
build(,tot,);
while(q--){
scanf("%d%d",&l,&r);
if(ans[l]==ans[r]) printf("%d\n",r-l+);
else printf("%d\n",max(query(ans[l]+,ans[r]-,,tot,),max(r-lx[ans[r]]+,rx[ans[l]]-l+)));
//同样,端点处单独考虑
}
}
return ;
}

最新文章

  1. ORACLE 物理读 逻辑读 一致性读 当前模式读总结浅析
  2. Python打包成exe程序
  3. WPF基础知识、界面布局及控件Binding
  4. [原创]java WEB学习笔记64:Struts2学习之路--主题
  5. pku1273 Drainage Ditches
  6. 清除oracl中有主外键关联的表中的部分数据。
  7. 解决Ajax.BeginForm还是刷新页面的问题
  8. 【RL-TCPnet网络教程】第35章 FTP文件传输协议基础知识
  9. JQuery EasyUI 之 messager基本使用
  10. weblogic 安装部署详解
  11. WebSocket实现一个聊天室
  12. python windows环境下文档备份
  13. 简单的SpringWebFlow例子及遇到的问题
  14. Solr——Windows下部署Solr7.5.0至jetty、Tomcat
  15. LeetCode 7 Reverse Integer &amp; int
  16. MVC应用程序显示上传的图片(续)
  17. SQL Server默认1433端口修改方法
  18. Nginx - 隐藏或修改版本号
  19. Quartz 2D中CGContextSaveGState与UIGraphicsPushContext
  20. ansys14.0 从入门到精通

热门文章

  1. [terry笔记]11gR2_dataguard_保护模式切换
  2. POJ——T 3687 Labeling Balls
  3. hdu3468 Treasure Hunting 二分匹配
  4. 9、包、访问控制、import、static、static代码块、final、抽象类、接口、instanceof、多态
  5. JAVA学习第四十六课 — 其它对象API(二)Date类 &amp;amp; Calendar类(重点掌握)
  6. 2014 百度之星 1003 题解 Xor Sum
  7. BootStrap有用代码片段(持续总结)
  8. Lambert/Diffuse 光照模型
  9. Delphi的时间 x87 fpu control word 精度设置的不够
  10. xp秘钥