zoukankan      html  css  js  c++  java
  • RMQ算法

    转自:https://www.cnblogs.com/yoke/p/6949838.html

    RMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题:对于一个长度N的数组,在多次询问中,每次都以O(1)的时间得到区间[a, b]的最大值或最小值。

    这个问题当然也能用线段树解决

    在线算法

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

    本文的ST算法预处理的时间是(NlogN)。

    离线算法

    所谓离线算法只是在来了非常多的请求之后,一次性处理多个请求,能够不依赖于预处理数据,却能一次完成多个请求的结果。

    ST ( Sparse Table ) 算法

    ST(Sparse Table)算法是一个非常有名的在线处理RMQ问题的算法,它可以在O(nlogn)时间内进行预处理,然后在O(1)时间内回答每个查询。其思想就是保存以i为起点的某段数据的最小值。

    预处理数据

    用DP思想来解决

    设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])。

     1 void RMQ(int n)
     2 {
     3     for (int j = 1; j < 20; j++) // 这里j的范围根据具体题目数据定义
     4         for (int i = 1; i <= n; i++)// n为数组内整数的个数
     5             if (i + (1 << j) - 1 <= n)
     6             {
     7                 maxsum[i][j] = max(maxsum[i][j - 1], maxsum[i + (1 << (j - 1))][j - 1]);
     8                 minsum[i][j] = min(minsum[i][j - 1], minsum[i + (1 << (j - 1))][j - 1]);
     9             }
    10 }

     考虑一下 为什么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]);

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

    练练手:

    Balanced Lineup

    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
    题意:给一个数列a,求再给m次询问,l,r区间内最大值和最小值的差值
     1 #include<iostream>
     2 #include<algorithm>
     3 #include <cmath>
     4 #include<cstdio>
     5 using namespace std;
     6 typedef long long ll;
     7 typedef unsigned long long ull;
     8 #define INF 0x3f3f3f3f
     9 const ll MAXN = 1e5 + 7;
    10 const ll MOD = 1e9 + 7;
    11 const double pi = acos(-1);
    12 int maxnum[MAXN][31];
    13 int minnum[MAXN][31];
    14 void RMQ(int n)
    15 {
    16     for (int j = 1; (1 << j) <= n; j++) // 这里j的范围根据具体题目数据定义
    17         for (int i = 1; i <= n; i++)    // n为数组内整数的个数
    18         {
    19             maxnum[i][j] = max(maxnum[i][j - 1], maxnum[i + (1 << (j - 1))][j - 1]);
    20             minnum[i][j] = min(minnum[i][j - 1], minnum[i + (1 << (j - 1))][j - 1]);
    21         }
    22 }
    23 int main()
    24 {
    25     int n, m;
    26     while (~scanf("%d%d",&n,&m))
    27     {
    28         for (int i = 1; i <= n; i++)
    29         {
    30             int num;
    31             scanf("%d",&num);
    32             maxnum[i][0] = minnum[i][0] = num;
    33         }
    34         RMQ(n);
    35         while(m--)
    36         {
    37             int l,r;
    38             scanf("%d%d",&l,&r);
    39             int cnt = 0;
    40             while ((1 << (cnt + 1)) <= r - l + 1)
    41                 cnt++;
    42            printf("%d
    ",max(maxnum[l][cnt], maxnum[r - (1 << cnt) + 1][cnt]) - min(minnum[l][cnt], minnum[r - (1 << cnt) + 1][cnt]));  
    43         }
    44     }
    45     return 0;
    46 }
    View Code
  • 相关阅读:
    vim 去掉自动注释和自动回车
    性别回归
    表情识别
    python list按字典的key值排序
    pytorch学习率策略
    python将list元素转为数字
    php面向对象
    mysql
    mysql
    mysql
  • 原文地址:https://www.cnblogs.com/graytido/p/10742298.html
Copyright © 2011-2022 走看看