"Go" about LIS and a class of DP prep knowledge that can be optimized with a tree-like array

Source: Internet
Author: User

Original link http://www.cnblogs.com/liu-runda/p/6193690.html

    1. Pre-knowledge

DP (Dynamic programming): An algorithm based on a state transfer without any validity, which we can first interpret as recursive. For example, the recursive method of the Fibonacci sequence can be considered not strictly DP. Of course, the state of DP can also be two-dimensional/three-dimensional, the meaning of a certain dimension is not only refers to a series of the first few items.

Tree Array (BIT or Fenwick tree): A data structure that dynamically maintains a sequence and dynamically evaluates prefixes. Modify an element/prefix and time complexity are O (LOGN)

2 LIS

    Okay, now let's assume you're all going to hit a tree-like array (if not, Baidu!) By the way the line tree also self-taught it! If you do not learn the tree-like array only the segment tree self-taught also as if there is no tree-like array capable and the line does not trunk things, let us do an example practice it!

Give a sequence of length n, find the LIS length (lis,longest increasing sequence, the longest ascending subsequence, the subsequence is defined as a number that is not necessarily adjacent but retains the original relative order from the original sequence, Ascending sub-sequence satisfies the number of the previous number in the subsequence is strictly less than the following numbers)

50% n<=5000,100% n<=50000

50% of the data does not actually use a tree-like array.

O (n^2) LIS algorithm: Defines f[i] is the length of the LIS when the end of item I is forced. Then from 1 to N to find each f[i],f[i] can be launched by f[1...i-1].

Well, now suppose you all write 50 points. We're thinking about optimizing the algorithm to run 50000.

First Discretization, the original number corresponds to the 1-tot (tot for the number of occurrences of the type), such as 10,10,20,15 into 1,1,3,2

Then we define g[i] as the LIS length ending with the number I, and scan the original array from left to right, while maintaining the G array. When scanning 1,1,3,2, the G array changes as follows: (the value of an item that does not appear is 0)

A.g[1]=1

B.g[1]=1

c.g[1]=1,g[3]=2

d.g[1]=1,g[2]=2,g[3]=2

yy How do we use the G array to complete the DP.

Next we need a data structure that supports single-point modification (Increase-only) and prefix-max-value queries.

First, a tease score block to maintain the G array: the G are divided into SZ block, maintain the maximum value of each piece. Modifications can be O (1) completed, the maximum value of the prefix query can be the entire block of the whole block with the maximum value of each block, scattered part of the violence swept over. SZ is the most efficient when taken near sqrt (n). Total time Complexity O (nsqrt (n)), confident AC.

What if n<=233333?

We can maintain a tree array of g arrays. The original tree array can be modified to support single-point modification (only increase and decrease) and prefix maximum value query. The addition of the tree array to take the maximum value, yy a bit.

The final maximum value of the entire G array is the answer.

  3. LCS

    Well, now suppose you all nlogn the LIS, let's have an example practice!

uva10635 Prince and Princess (data range is my mouth)

Given the arrangement of two 1~n (in a 1~n arrangement, 1-n each number appears once and does not recur), the length of the LCS (the longest common subsequence) is obtained. 50% of the data guarantees that the first permutation is 1,2,3,...N in sequential increments.

n<=233333

50% data is the second arranged Lis. Very good 50 points.

Consider the following, renumber the numbers, the length of the LCS unchanged. For example, two sequences are three-to-five; 3,2,1. So the a,b,c;c,b,a answer is the same as it. It is also possible to number numbers as numbers.

For example, these two data:

(1)

4

1 2 3 4

2 3 4 1

(2)

4

4 3 2 1

3 2 1 4

Their answers are the same. Because when we solve LCS, we focus only on whether the values of the two subscripts in the two sequences are the same, and renumber the information that does not change the "number of corresponding subscript positions".

Then we renumber a sequence to 1,2,3...N, and another sequence is renumbered with the same correspondence (that is, each number in the second sequence is changed to the subscript that appears in the first sequence), running Lis, confident AC.

  4. More about LCS

Next we suited a surprise attack, using another way of thinking to get another nlogn approach to the example LCS.

First oneself yy the LCS O (n^2) algorithm. Its state is defined as f[i][j], which means the first I element of the second sequence is allowed to be used and the first J element (where I, the first J element is not required)

Next we modify the state definition (something that needs to be done frequently when optimizing DP) so that it can be optimized to NLOGN.

In the new state, F[i][j] represents the first I element of the second sequence that is allowed to be used, and the first J element of the next series (where element i is not required to use, and the second sequence of J elements must be used )

So we changed the original DP to a layered process, considering the f[i][from f[i-1][], then the two arrays would have at most one position different (only the answer that ends with the first element of the second sequence will change).

Then we can dynamically maintain this array. If the element I of the first sequence appears in the second sequence in the K-position, then modify F[k] (the first dimension has been eliminated here).

F[k] equals what? equals F[1...k-1] is the maximum value of +1, because the previous item in the LCS must be one of the items in front of the second sequence (if it is already the first item, there is no previous item) k-1 I don't (bother) writing the border by myself.

This is the tree array prefix maximum value, only increase the single point of modification.

In fact, this algorithm only guarantees that the matching position is not much. Therefore 1-n each number appears 5 times also can write.

http://www.lydsy.com/JudgeOnline/problem.php?id=1264 bzoj1264 Gene Matching

  5. More about BIT&DP

    Two-dimensional tree arrays can also be used to optimize DP http://www.lydsy.com/JudgeOnline/problem.php?id=3594 's corn field

Coordinate system can go ... http://www.lydsy.com/JudgeOnline/problem.php?id=2131 free pies

and a tree array it doesn't matter what a stupid question http://www.lydsy.com/JudgeOnline/problem.php?id=4300

2016.12.31update: Finally thought of a noip problem NOIP2012 flower http://cogs.pro/cogs/problem/problem.php?pid=1270

  6. Consolidate and practise lis

    http://www.lydsy.com/JudgeOnline/problem.php?id=3173

http://www.lydsy.com/JudgeOnline/problem.php?id=4660

Because I am too weak, so the examples here are relatively few ... Humbly ask Dalao to provide examples in the comment area.

"Go" about LIS and a class of DP prep knowledge that can be optimized with a tree-like array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.