POJ3264 Balanced Lineup

本文介绍了一种使用ST算法解决区间最值问题的方法。该算法通过预处理数组,实现快速查询任意区间内的最大值和最小值,适用于解决类似于奶牛高度差的问题。通过实例演示了算法的具体实现过程。

 

Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 44720 Accepted: 20995
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

Source

 

ST算法求区间内最值

 

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 const int mxn=60000;
 8 int n,Q;
 9 int h[mxn];
10 int fmx[mxn][20],fmi[mxn][20];//f[i][j]表示从i开始到i*(1<<j)范围内的目标值 
11 int ST(int a,int b){//ST算法,倍增求区间最值 
12     int i,j;
13     for(i=1;i<=n;i++){//自身 
14         fmx[i][0]=fmi[i][0]=h[i];
15     }
16     int k=(int)(log(n*1.0)/log(2.0));//k=以2为底n的对数 
17     for(i=1;i<=k;i++){//第一层为次数 
18         for(j=1;j<=n;j++){//第二层为范围 
19             fmx[j][i]=fmx[j][i-1];
20             if(j+(1<<(i-1)) <=n)
21                 fmx[j][i]=max(fmx[j][i],fmx[j+(1<<(i-1))][i-1]);
22             fmi[j][i]=fmi[j][i-1];
23             if(j+(1<<(i-1)) <=n)
24                 fmi[j][i]=min(fmi[j][i],fmi[j+(1<<(i-1))][i-1]);
25         }
26     }
27     return 0;
28 }
29 int ansmx(int a,int b){//查找最大值 
30     int k=(int)(log(b-a+1.0)/log(2.0));
31     return max(fmx[a][k],fmx[b-(1<<k)+1][k]);
32 }
33 int ansmi(int a,int b){//查找最小值 
34     int k=(int)(log(b-a+1.0)/log(2.0));
35     return min(fmi[a][k],fmi[b-(1<<k)+1][k]);
36 }
37 int main(){
38     scanf("%d%d",&n,&Q);
39     int i,j;
40     for(i=1;i<=n;i++){
41         scanf("%d",&h[i]);
42     }
43     int a,b;
44     ST(1,n);
45     for(i=1;i<=Q;i++){
46         scanf("%d%d",&a,&b);
47         printf("%d\n",ansmx(a,b)-ansmi(a,b));//输出询问区间内最大值和最小值的差 
48     }
49     return 0;
50 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/5638900.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值