E - St表/线段树 POJ - 3264Balanced Lineup【RMQ模版题】

1.题目
E - St表/线段树 POJ - 3264
Balanced Lineup【RMQ模版题】一段范围内的最大值与最小值之差
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

2.题目大意
给出N个数字,求某个区间中最大值和最小值的差

3.解题思路
这是RMQ模版题,RMQ问题是指求区间最值的问题。那么既然能求出最大值和最小值,那么差值也就解决了。

4.ST算法
A.预处理

运用DP,得到的是F[i,j]=max(F[i,j-1],F[i+1<<(j-1),j-1])这个式子.
过程:用f[i,j]表示从第i个数起连续2^j个数中的最大值。
其中我们把f[i,j](j≥1)原本的一段数字中平均分成两段
(因为j≥1时,f[i,j]一定是偶数个连续数字中的最大值),从i到i+2^(j-1)-1为一段,
i+2^(j-1)到i+2^j-1为一段。
void initST()
{
    for(int i = 1; i <= n; i++ )
    {
        RMQ_max[i][0] = RMQ_min[i][0] = a[i];
    }
    for(int i = 1; (1<<i) <= n; i++)
    {
        for(int j = 1; j+(1<<i)-1 <= n; j++)//j+(1<<i)-1<=n是为了保证区间左端点不超出总数n
        {
            RMQ_max[j][i] = max(RMQ_max[j][i-1], RMQ_max[j+(1<<(i-1))][i-1]);
            RMQ_min[j][i]  = min(RMQ_min[j][i-1], RMQ_min[j+(1<<(i-1))][i-1]);
        }
    }
  }

B.得出结果

假设我们要求区间[2,8]的最大值,就要把它分成[2,5][5,8]两个区间,
因为这两个区间的最大值我们可以直接由f[2,2]和f[5,2]得到。
扩展到一般情况,就是把区间[l,r]分成两个长度为2^n的区间(保证有f对应)。
直接给出表达式:
k:int k=int (log(y-z+1)/log(2));//注意y-z要加一才为区间长度
//或者while(1<<(k+1)<= R-L+1) k++;
ans:=max(F[l,k],F[r-2^k+1,k]);

int getans(int L, int R)
{
    int k=0 ;
    while(1<<(k+1)<= R-L+1) k++;
    int max1 = max(RMQ_max[L][k],RMQ_max[R-(1<<k)+1][k]);
    int min1 = min(RMQ_min[L][k],RMQ_min[R-(1<<k)+1][k]);
    return max1-min1;
}

C.详细学习
https://www.cnblogs.com/ljy-endl/p/11331921.html

5.AC代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define  N 50050
using namespace std;

int a[N];
int n;
int RMQ_max[N][20],RMQ_min[N][20];
void initRMQ()
{
    for(int i = 1; i <= n; i++ )
    {
        RMQ_max[i][0] = RMQ_min[i][0] = a[i];
    }
    for(int i = 1; (1<<i) <= n; i++)
    {
        for(int j = 1; j+(1<<i)-1 <= n; j++)
        {
            RMQ_max[j][i] = max(RMQ_max[j][i-1], RMQ_max[j+(1<<(i-1))][i-1]);
            RMQ_min[j][i]  = min(RMQ_min[j][i-1], RMQ_min[j+(1<<(i-1))][i-1]);
        }
    }
}

int getans(int L, int R)
{
    int k=0 ;
    while(1<<(k+1)<= R-L+1) k++;
    int max1 = max(RMQ_max[L][k],RMQ_max[R-(1<<k)+1][k]);
    int min1 = min(RMQ_min[L][k],RMQ_min[R-(1<<k)+1][k]);
    return max1-min1;
}
int main()
{
     int q; scanf("%d%d", &n,&q);
     for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
     initRMQ();
          for(int i = 0; i < q; i++)
     {
         int L,R; scanf("%d%d",&L,&R);
        printf("%d\n",getans(L,R));
     }
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值