HDU 1025 longest increasing subsequence
Constructing Roads In JGShining’s Kingdom
The meaning of the problem
So why this is longest increasing subsequence Problem ? Look at the picture below:
As the problem described, we can’t have crossed lines. Each line has two endpoints. One is the “rich” endpoint above and the other is the “poor” endpoint below. The rich endpoints’ number of the lines we finally pick up should be monotonically increasing. The corresponding poor endpoints should be monotonically increasing too. Otherwise the lines will be crossed.
Let’s use a[p]=q
to represent that the p city can link q city. Then the problem tend to become a question of longest increasing subsequence.
The Algorithm
There is a quite easy way to understand the LIS problem. I learned it from this tutorial
A clever approach. Create a new array
cell
to store the longest increasing subsequence.Iterate through the original sequence, and binary insert each element into
cell
.If all the elements in
cell
are smaller than the current one, append it at the end.
Otherwise, replace the smallest element incell
that is larger than the current one.
In short, the idea is to keepcell
storing relatively smaller elements. Althoughcell
may not represent the actual longest increasing subsequence, its length is correct.Author: ColdMe
Link: https://leetcode.cn/problems/longest-increasing-subsequence/solutions/13118/zui-chang-shang-sheng-zi-xu-lie-dong-tai-gui-hua-e/
Source: LeetCode
Copyright belongs to the author. For commercial use, please contact the author for authorization. For non-commercial use, please credit the source.
There are ways to prove that the cell
array is monotonically increasing. Anything detailed you can see Wikipedia
Code
1 |
|