RMQ STアルゴリズムについて

4186 ワード

RMQ(Range Minimum/Maximum Query)           。

          ST  

                      ,       n,              O(n),         ,         ,             ,    DP  , DP[i][j]   i                。

       ,     arr[5] = {5, 5, 5, 2, 1},  DP[i][0]   a[i],  ,                 ,   DP[i][1]      arr[i],arr[i + 1]        。        DP[i][0] DP[i][1]    ,    DP[i][2]  DP[i][1] DP[i + 2][1],        DP[i][j]   DP[i][j - 1] DP[i + (1 << j)][j - 1]   ,         DP  。                       ,      ,          n  log2(n)     ,     [l, r]   ,   ,    ,       DP[l][k] DP[r - (1 << k ) + 1][k]   。   ST       ,             。                。

  ST           ,      DP       ,DP[i][j] i          n, j    。     DP         i + 1 << j         ,       break        

                  。

Balanced Lineup
Time Limit: 5000MS
Memory Limit: 65536K
Total Submissions: 45880
Accepted: 21556
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つのシーケンスを与え、その後、複数のクエリがあり、各クエリが与える区間内の最大値の最小値の差を求めることを意味する.
方法はSTアルゴリズムを用いて区間の最大値と最小値の2つのDP配列を維持し,次いで2つの最値を求める関数を設計すればよい.
コードは次のとおりです.
/*************************************************************************
	> File Name: Balanced_Lineup.cpp
	> Author: ZhangHaoRan
	> Mail: [email protected]
	> Created Time: 2016 07 14      09 52 03 
 ************************************************************************/

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int N = 50010;
int dh[N][17], dl[N][17];

int GetMax(int l, int r){
    int k = log2(r - l + 1);
    return max(dh[l][k], dh[r - (1 << k) + 1][k]);
}

int GetMin(int l , int r){
    int k = log2(r - l  + 1);
    return min(dl[l][k], dl[r - (1 << k) + 1][k]);
}

int n, q;
int main(void){
    cin >> n >> q;
    int temp;
    for(int i = 1; i <= n; i ++){
        scanf("%d", &temp);
        dl[i][0] = temp;
        dh[i][0] = temp;
    }
    for(int i = 1; i <= 16; i ++){
        for(int j = 1; j <= n; j ++){
            if(j + (1 << (i - 1)) <= n){
                dh[j][i] = max(dh[j][i - 1], dh[j + (1 << (i - 1))][i - 1]);
                dl[j][i] = min(dl[j][i - 1], dl[j + (1 << (i - 1))][i - 1]);
            }
            else 
                break;
        }
    }
    int l, r;
    for(int i = 0; i < q; i ++){
        scanf("%d %d", &l, &r);

        cout << GetMax(l, r) - GetMin(l, r) << endl;
    }
    
    return 0;
}

原文を参照:http://chilumanxi.org/2016/07/27/%e6%b5%85%e8%b0%88rmq-st%e7%ae%97%e6%b3%95/