• RMQ算法详解


    RMQ算法,是一个快速求区间最值的离线算法,预处理时间复杂度O(n*log(n)),查询O(1),所以是一个很快速的算法。

    当然这个问题用线段树同样能够解决,算法复杂度为:O(N)~O(logN) 。

    RMQ:

    RMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题:对于长度为n的数列A,

    回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j之间的最小/大值。

    分析:

    对于该问题,最容易想到的解决方案是遍历,复杂度是O(n)。但当数据量非常大且查询很频繁时,该算法无法在有效的时间内查询出正解。

    本节介绍了一种比较高效的在线算法(ST算法)解决这个问题。所谓在线算法,是指用户每输入一个查询便马上处理一个查询。该算法一般用较长的时间做预处理,待信息充足以后便可以用较少的时间回答每个查询。ST(Sparse Table)算法是一个非常有名的在线处理RMQ问题的算法

    预处理:

    设A[i]是要求区间最值的数列,F[i, j]表示从第i个数起连续2^j个数中的最大值。(DP的状态)

    例如:

    A数列为:3 2 4 5 6 8 1 2 9 7

    F[1,0]表示第1个数起,长度为2^0=1的最大值,其实就是3这个数。同理 F[1,1] = max(3,2) = 3, F[1,2]=max(3,2,4,5) = 5,F[1,3] = max(3,2,4,5,6,8,1,2) = 8;

    并且我们可以容易的看出F[i,0]就等于A[i]。(DP的初始值)

    这样,DP的状态、初值都已经有了,剩下的就是状态转移方程。

    我们把F[i,j]平均分成两段(因为f[i,j]一定是偶数个数字),从 i 到i + 2 ^ (j - 1) - 1为一段,i + 2 ^ (j - 1)到i + 2 ^ j - 1为一段

    (长度都为2 ^ (j - 1))。用上例说明,当i=1,j=3时就是3,2,4,5 和 6,8,1,2这两段。F[i,j]就是这两段各自最大值中的最大值。

    于是我们得到了状态转移方程F[i, j]=max(F[i,j-1], F[i + 2^(j-1),j-1])。

    void RMQ(int num) //预处理->O(nlogn)
    {
        for(int j = 1; j < 20; ++j)    // 这里j的范围根据具体题目数据定义
            for(int i = 1; i <= num; ++i)    // num为数组内整数的个数
                if(i + (1 << j) - 1 <= num)
                {
                    maxsum[i][j] = max(maxsum[i][j - 1], maxsum[i + (1 << (j - 1))][j - 1]);
                    minsum[i][j] = min(minsum[i][j - 1], minsum[i + (1 << (j - 1))][j - 1]);
                }
    }

    考虑一下 为什么j是外循环而i是内循环? 能不能调换一下嘞?

    答案是不可以。因为我们需要理解这个状态转移方程的意义。
    状态转移方程的含义是:先更新所有长度为F[i,0]即1个元素,然后通过2个1个元素的最值,获得所有长度为F[i,1]即2个元素的最值,然后再通过2个2个元素的最值,获得所有长度为F[i,2]即4个元素的最值,以此类推更新所有长度的最值。
    而如果是i在外,j在内的话,我们更新的顺序就是F[1,0],F[1,1],F[1,2],F[1,3],表示更新从1开始1个元素,2个元素,4个元素,8个元素(A[0],A[1],....A[7])的最值,这里F[1,3] = max(max(A[0],A[1],A[2],A[3]),max(A[4],A[5],A[6],A[7]))的值,但是我们根本没有计算max(A[0],A[1],A[2],A[3])和max(A[4],A[5],A[6],A[7]),所以这样的方法肯定是错误的。
    为了避免这样的错误,一定要好好理解这个状态转移方程所代表的含义。
    先自己考虑一下哈!

    查询:

    假如我们需要查询的区间为(i,j),那么我们需要找到覆盖这个闭区间(左边界取i,右边界取j)的最小幂

    (可以重复,比如查询5,6,7,8,9,我们可以查询5678和6789)。

    因为这个区间的长度为j - i + 1,所以我们可以取k=log2( j - i + 1),则有:RMQ(A, i, j)=max{F[i , k], F[ j - 2 ^ k + 1, k]}。

    举例说明,要求区间[2,8]的最大值,k = log2(8 - 2 + 1)= 2,即求max(F[2, 2],F[8 - 2 ^ 2 + 1, 2]) = max(F[2, 2],F[5, 2]);

    在这里我们也需要注意一个地方,就是<<运算符和+-运算符的优先级。

    实战一下:

    Time Limit: 5000MS   Memory Limit: 65536K
         
    Case Time Limit: 2000MS

    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

    Line 1: Two space-separated integers, N and Q
    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

    Lines 1..Q: Each line contains a single 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

     

    题目大意:

         输入一组整数,然后给定一下查询区间,输出查询区间内最大值与最小值的差值。

    代码:

     1 #include <stdio.h>
     2 #include <math.h>
     3 #include <string.h>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 int maxnum[50010][20];
     8 int minnum[50010][20];
     9 int main()
    10 {
    11     int num,n,q,i,j,x,y;
    12     while (~scanf("%d%d",&n,&q))
    13     {
    14         for (i = 1; i <= n; i ++)
    15         {
    16             scanf("%d",&num);
    17             maxnum[i][0] = minnum[i][0] = num;   // 先预处理2^0长度的 
    18         }
    19         
    20         for (j = 1; (1<<j) <= n; j ++)
    21          for (i = 1; i+(1<<j)-1 <= n; i ++)   // 预处理 
    22          {                                                 
    23             maxnum[i][j] = max(maxnum[i][j-1],maxnum[i + (1<<(j-1))][j-1]);
    24             minnum[i][j] = min(minnum[i][j-1],minnum[i + (1<<(j-1))][j-1]);
    25          }
    26             
    27         while (q --)
    28         {
    29             scanf("%d%d",&x,&y);
    30             int z = 0;
    31             while ((1<<(z+1)) <= y-x+1) z ++;
    32             int ans = max(maxnum[x][z],maxnum[y-(1<<z)+1][z])
    33                      - min(minnum[x][z],minnum[y-(1<<z)+1][z]);
    34             printf("%d
    ",ans);
    35         }
    36     }
    37     return 0;
    38 }

    本文参考:http://blog.csdn.net/liang5630/article/details/7917702

  • 相关阅读:
    【禅道】禅道安装步骤
    软件测试学习路线
    【mysql】mysql数据库安装
    【用例】测试用例阶段总结
    【坑】自动化测试之Excel表格
    开始.....
    网络攻防
    PAT/查找元素习题集
    PAT/简单模拟习题集(二)
    PAT/简单模拟习题集(一)
  • 原文地址:https://www.cnblogs.com/yoke/p/6949838.html
Copyright © 2020-2023  润新知