|
Posted on 2009-08-28 09:20 reiks 阅读(3497) 评论(0) 编辑 收藏 引用 所属分类: 算法与数据结构
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//*
RMQ(Range Minimum/Maximum Query)问题:
RMQ问题是求给定区间中的最值问题。当然,最简单的算法是O(n)的,但是对于查询次数很多(设置多大100万次),O(n)的算法效率不够。可以用线段树将算法优化到O(logn)(在线段树中保存线段的最值)。不过,Sparse_Table算法才是最好的:它可以在O(nlogn)的预处理以后实现O(1)的查询效率。下面把Sparse Table算法分成预处理和查询两部分来说明(以求最小值为例)。
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
预处理:
预处理使用DP的思想,f(i, j)表示[i, i+2^j - 1]区间中的最小值,我们可以开辟一个数组专门来保存f(i, j)的值。
例如,f(0, 0)表示[0,0]之间的最小值,就是num[0], f(0, 2)表示[0, 3]之间的最小值, f(2, 4)表示[2, 17]之间的最小值
注意, 因为f(i, j)可以由f(i, j - 1)和f(i+2^(j-1), j-1)导出, 而递推的初值(所有的f(i, 0) = i)都是已知的
所以我们可以采用自底向上的算法递推地给出所有符合条件的f(i, j)的值。
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
查询:
假设要查询从m到n这一段的最小值, 那么我们先求出一个最大的k, 使得k满足2^k <= (n - m + 1).
于是我们就可以把[m, n]分成两个(部分重叠的)长度为2^k的区间: [m, m+2^k-1], [n-2^k+1, n];
而我们之前已经求出了f(m, k)为[m, m+2^k-1]的最小值, f(n-2^k+1, k)为[n-2^k+1, n]的最小值
我们只要返回其中更小的那个, 就是我们想要的答案, 这个算法的时间复杂度是O(1)的.
例如, rmq(0, 11) = min(f(0, 3), f(4, 3))
*/
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include<iostream>
#include<cmath>
using namespace std;
#define MAXN 1000000
#define mmin(a, b) ((a)<=(b)?(a):(b))
#define mmax(a, b) ((a)>=(b)?(a):(b))
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int num[MAXN];
int f1[MAXN][100];
int f2[MAXN][100];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//测试输出所有的f(i, j)
void dump(int n)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int i, j;
for(i = 0; i < n; i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for(j = 0; i + (1<<j) - 1 < n; j++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
printf("f[%d, %d] = %d\t", i, j, f1[i][j]);
}
printf("\n");
}
for(i = 0; i < n; i++)
printf("%d ", num[i]);
printf("\n");
for(i = 0; i < n; i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for(j = 0; i + (1<<j) - 1 < n; j++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
printf("f[%d, %d] = %d\t", i, j, f2[i][j]);
}
printf("\n");
}
for(i = 0; i < n; i++)
printf("%d ", num[i]);
printf("\n");
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//sparse table算法
void st(int n)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int i, j, k, m;
k = (int) (log((double)n) / log(2.0));
for(i = 0; i < n; i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
f1[i][0] = num[i]; //递推的初值
f2[i][0] = num[i];
}
for(j = 1; j <= k; j++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { //自底向上递推
for(i = 0; i + (1 << j) - 1 < n; i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m = i + (1 << (j - 1)); //求出中间的那个值
f1[i][j] = mmax(f1[i][j-1], f1[m][j-1]);
f2[i][j] = mmin(f2[i][j-1], f2[m][j-1]);
}
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//查询i和j之间的最值,注意i是从0开始的
void rmq(int i, int j)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int k = (int)(log(double(j-i+1)) / log(2.0)), t1, t2; //用对2去对数的方法求出k
t1 = mmax(f1[i][k], f1[j - (1<<k) + 1][k]);
t2 = mmin(f2[i][k], f2[j - (1<<k) + 1][k]);
printf("%d\n",t1 - t2);
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int i,N,Q,A,B;
scanf("%d %d", &N, &Q);
for (i = 0; i < N; ++i)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
scanf("%d", num+i);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
st(N); //初始化
//dump(N); //测试输出所有f(i, j)
while(Q--)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
scanf("%d %d",&A,&B);
rmq(A-1, B-1);
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
|