How does the algorithm work for the largest increasing subsequence [O (nlogn)]?

I found the algorithm mentioned in Hitchhiking Programming (note: this implementation assumes there are no duplicates in the list):

set<int> st;
set<int>::iterator it;
st.clear();

for(i=0; i<n; i++) {

  st.insert(array[i]); it=st.find(array[i]);

  it++; if(it!=st.end()) st.erase(it);
}

cout<<st.size()<<endl;

This is an algorithm for finding the longest increasing subsequence in O (NlogN). If I try to work with a few test cases, this will work. But I still could not understand his logic of correctness. Also, it doesn't look so intuitive to me.

Can someone help me understand why this algorithm works correctly?

+4
source share
2 answers

?

, . , :

LIS . , LIS. , , , , .

+2

: .

: :

: .

, i-1, - LIS [i-1], .. LIS i-1 .

: [i] ​​ .

  • A [i] >= set.last(): A [i] , , LIS [i] = LIS [i-1] +1.

  • A [i] set.last(): A [i] ​​ , A [i] ​​ . LIS [i] = LIS [i-1] + 1 ( A [i]) - 1 ( a > A [i]). . , .

. A [i] ​​ ​​ LIS [i-1], LIS, 0- i- .

+4

All Articles