st算法――RMQ的一个简单可行的算法 - QuarterGeek



st算法――RMQ的一个简单可行的算法 - QuarterGeek

t算法――RMQ的一个简单可行的算法

RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就是说,RMQ问题是指求区间最值的问题。――百度百科

求出数组中任意区间[l,r]的最值,朴素的算法需要O(n)的时间。当询问特别多时,效率十分低。 st算法可以在O(nlogn)的时间内做完预处理,然后用O(1)的时间回答每一个问题。具体实现方法如下:(其实是一个动规^_^) 以求最大值为例。 用f[i,j]表示区间[i,i+2j-1]中的最大值。那么: f[i,j]=max(F[i,j-1],F[i+2j-1,j-1]) 预处理完后,对于询问求[a,b]的最大值。我们首先需要求出一个最大的整数x满足2x <=r-l+1那么,答案就是max(f[a,x],f[b+1-2x,x])。

由于pascal中,log2函数在math库中,使用还需uses math; 而ln则不需要使用任何的库,所以可以考虑换底公式: log a(b)=log c(b)/log c(a) 也就是log2(a)=ln(a)/ln(2)

例题如:Balanced Lineup,USACO 2007 January Silver(即PKU3264)。求区间的最大值-最小值。程序如下:


Read full article from st算法――RMQ的一个简单可行的算法 - QuarterGeek


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts