1 次元の問題をどのように解決するかを考えてみましょう。
dp[i] = longest increasing subsequence that ends at position i.
ごとに、最大であるようなものi
に追加a[i]
する必要があります。配列の定義を変更することにより、高度なデータ構造を使用してこれを効率的に見つけることができます。j
dp[j]
a[j] < a[i]
dp
dp[i] = longest increasing subsequence that ends with the VALUE i
さて、最初dp[ a[i] ] = 1
はそれぞれの位置についてi
. 次に、各要素a[i]
について、最大の が必要ですdp[0], dp[1], ..., dp[ a[i] - 1 ]
。次に、あなたが持っていdp[ a[i] ] = mx + 1
ます。
You can find this maximum by normalizing your array values (make sure they are all in the interval [1, n]
). You can do this with a sort. For example, if your array is 42 562 13
, the n = 3
and your new array will be 2 3 1
.
This can be implemented easily with a segment tree that supports queries and updates for maximums. The time complexity will be O(n log n)
.
This can be extended quite easily to your problem by using 2D segment trees, obtaining time complexity O(n log^2 n)
, which should not be too complex at all. This involves using a segment tree whose nodes are also segment trees.