Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 27567 | Accepted: 12955 | |
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
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
Sample Input
6 3 1 7 3 4 2 5 1 5 4 6 2 2
Sample Output
6 3 0
Source
- ** RMQ(Range Minimum/Maximum Query)问题:
- ** RMQ问题是求给定区间中的最值问题。当然,最简单的算法是O(n)的,但是对于查询次数很多(设置多大100万次),O(n)的算法效率不够。
- ** 可以用线段树将算法优化到O(logn)(在线段树中保存线段的最值)。不过,Sparse_Table算法才是最好的:
- ** 它可以在O(nlogn)的预处理以后实现O(1)的查询效率。下面把Sparse Table算法分成预处理和查询两部分来说明(以求最小值为例)。
- ** 预处理:
- ** 预处理使用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)的值。
- ** 查询:
- ** 假设要查询从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))
- ** 由此我们要注意的是预处理f(i,j)中的j值只需要计算log(n+1)/log(2)即可,而i值我们也只需要计算到n-2^k+1即可。
- 代码:
-
#include <iostream> #include <cmath> #define mmax(a,b) ((a)>(b)?(a):(b)) #define mmin(a,b) ((a)<(b)?(a):(b)) #define MAX 1000005 using namespace std; int num[MAX],ffmin[MAX][50],ffmax[MAX][50]; void st(int n) { int i,j,k; for(i=0; i<n; i++) { ffmin[i][0]=num[i]; ffmax[i][0]=num[i]; } k=(int)(log((double)(n))/log(2.0)); for(j=1; j<=k; j++) { for(i=0; i+(1<<j)-1<n; i++) { ffmin[i][j]=mmin(ffmin[i][j-1],ffmin[i+(1<<(j-1))][j-1]); ffmax[i][j]=mmax(ffmax[i][j-1],ffmax[i+(1<<(j-1))][j-1]); } } } void rmq(int i,int j) { int k=(int)(log((double)(j-i+1))/log(2.0)); int t1,t2; t1=mmax(ffmax[i][k],ffmax[j-(1<<k)+1][k]); t2=mmin(ffmin[i][k],ffmin[j-(1<<k)+1][k]); cout<<t1-t2<<endl; } int main() { int n,q,a,b,i; cin>>n>>q; for(i=0; i<n; i++) cin>>num[i]; st(n); while(q--) { cin>>a>>b; rmq(a-1,b-1); } return 0; }
FAQ | About Virtual Judge | Forum | Discuss | Open Source Project
All Copyright Reserved ©2010-2012 HUST ACM/ICPC TEAM
Anything about the OJ, please ask in the forum, or contact author: Isun
Server Time: 2015-01-29 15:31:21
Disqus seems to be taking longer than usual. Reload?