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

RMQ算法 以及UVA 11235 Frequent Values(RMQ)的更多相关文章

  1. UVA 11235 Frequent Values ---RMQ

    大白书上的例题,具体讲解见大白书,最好用用一个Log数组直接求k,这样就是纯O(1)了 #include <iostream> #include <cstdio> #inclu ...

  2. UVa 11235 Frequent values (RMQ && 区间出现最多次的数的次数)

    题意 : 给出一个长度为 n 的不降序序列,并且给出 q 个形如(L, R)的问询,问你这个区间出现的最多次的数的次数. 分析 : 很自然的想到将区间“缩小”,例如1 1 2 3 3 3就可以变成2 ...

  3. UVA 11235 Frequent values(RMQ)

    Frequent values TimeLimit:3000Ms , ... , an in non-decreasing order. In addition to that, you are gi ...

  4. UVA 11235 Frequent values 线段树/RMQ

    vjudge 上题目链接:UVA 11235 *******************************************************大白书上解释**************** ...

  5. [POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]

    2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...

  6. UVA - 11235 Frequent values

    2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...

  7. POJ 3368 & UVA 11235 - Frequent values

    题目链接:http://poj.org/problem?id=3368 RMQ应用题. 解题思路参考:http://blog.csdn.net/libin56842/article/details/4 ...

  8. POJ 3368 Frequent values RMQ ST算法/线段树

                                                         Frequent values Time Limit: 2000MS   Memory Lim ...

  9. 数据结构(RMQ):UVAoj 11235 Frequent values

    Frequent values You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. I ...

随机推荐

  1. httpd: Could not reliably determine the server&#39;s fully qualified domain name

    [root@luozhonghua sbin]# service httpd start Starting httpd: httpd: apr_sockaddr_info_get() failed f ...

  2. 【BZOJ 1660】 [Usaco2006 Nov]Bad Hair Day 乱发节

    1660: [Usaco2006 Nov]Bad Hair Day 乱发节 Time Limit: 2 Sec  Memory Limit: 64 MB Submit: 678  Solved: 32 ...

  3. 英语音乐---三、Cry on my shoulder

    英语音乐---三.Cry on my shoulder 一.总结 一句话总结:Cry on my shoulder 在我的肩膀上哭泣 1.If the hero never comes to you. ...

  4. DNS解析污染原理——要么修改包,要么直接丢弃你的网络包

    DNS/域名解析 可以看到dns解析是最初的一步,也是最重要的一步.比如访问亲友,要知道他的正确的住址,才能正确地上门拜访. dns有两种协议,一种是UDP(默认),一种是TCP. udp 方式,先回 ...

  5. 实时监控Cat之旅~对Get和Post进行封装,支持分布式消息树

    对第三方接口的调用我们需要对GET和POST进行监控,看一些请求的执行是否成功,如A调用B,B调用C,C调用D,这一连串的东西需要我们使用cat进行记录,进行记录之后,我们可以很容易的发现请求响应的时 ...

  6. 1570. [POJ3461]乌力波

    ★☆   输入文件:oulipo.in   输出文件:oulipo.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 法国作家乔治·佩雷克(Georges Perec,1 ...

  7. JavaScript总结(3)

    第3章 获取用户的输入 <script>10 intA=prompt("请输入第一个数字","");11 intB=prompt("请输入 ...

  8. 深入理解JavaScript定时机制

    容易欺骗别人感情的JavaScript定时器 JavaScript的setTimeout与setInterval是两个很容易欺骗别人感情的方法,因为我们开始常常以为调用了就会按既定的方式执行, 我想不 ...

  9. Oracle 流程控制语句

    分为选择语句循环语句两大类:一 选择语句1 if then ...end;set serveroutput on declare var_name1 varchar2(50):='East'; var ...

  10. UI Framework-1: Aura Views

    Views Views is a user interface framework built on a type called, confusingly, View. Responsible for ...