Opinion ST

ST algorithm flow

Pretreatment

  ST dynamic programming algorithm in fact, we use an array to represent a set of numbers set f [i] [j] represents the a [i] to start a [i + 2 ^ j - 1] maximum in this range, i.e., start continuous 2 ^ j the largest number from the number of position i. Since the number of elements of two 2 ^ j, so we can divide it into two parts, each part length of 2 ^ (j-1), that is, we have I f [i, j] is divided into f [i, j - 1] and f [i + 2 ^ (j - 1), j - 1], as shown:

  State transition equation entire interval apparent as: f [i] [j] = max (f [i] [j - 1], f [i + 2 ^ (j - 1)] [j - 1]), the boundary condition is f [i] [0] = a [i], so that we can time a degree of complexity O (nlogn) within a processing array f

ask

  If we need, the maximum value of the interval [l, r], we need to meet the requirements of the greatest x 2 ^ x <= r - l +1, then, becomes the interval [l, l + 2 ^ x - 1 ] and [l + 2 ^ x, r] in two parts, as shown:

  Elements of the two sections are 2 ^ x, so that the interval [l, r] is the maximum value max (f [l] [x], f [r - 2 ^ x + 1] [x]), in O computing (1), the ST is a speed faster than the tree line to the algorithm, but can not be modified ST, ST algorithm that is suitable only for the reason for seeking the value of the interval

  Find the interval [x, y] maximum expression is:

  k = log2(y - x + 1)

  years = max (f [x] [k], f [y - 2 ^ k + 1] [k])

template

 

Guess you like

Origin www.cnblogs.com/-sheldon/p/11409065.html