Longest Increasing Subsequence
http://www.algorithmist.com/index.php/Longest_Increasing_Subsequence
Given a sequence , find the largest subset such that for every i < j, ai < aj.
O(nlogn) solution
Let Ai,j be the smallest possible tail out of all increasing subsequences of length j using elements .
Ai,j 在这里表示原数组里长度为j的递增子序列的可能的最小的末尾(例:对j=2有“14”和“13”则Ai,2表示3)
for any particular i, 这是观察所得规律,因其单增,故可用二分法
This suggests that if we want the longest subsequence that ends with ai + 1, we only need to look for a j such that Ai,j < ai + 1 < = Ai,j + 1 and the length will be j + 1.
Notice that in this case, Ai + 1,j + 1 will be equal to ai + 1, and all Ai + 1,k will be equal to Ai,k for .
Furthermore, there is at most one difference between the set Ai and the set Ai + 1, which is caused by this search.
Since A is always ordered in increasing order, and the operation does not change this ordering, we can do a binary search for every single .
Further explain:--GONG Zhi Tao 11:19, 1 August 2012 (EDT)
We have elements: .
And we have a longest increasing subsequences of them: , for any Ai,k(1 < = k < = j) you could not find a smaller alternative.
Now we have a new element: ai + 1
What we can do about it:
1. insert it at the back if Ai,j < ai + 1, where we will have a longer one;
2. make it an alternative for Ai,k if
Alternative means that we MIGHT get longer ones if using the new element.
http://blog.csdn.net/linulysses/article/details/5559262
具体实现代码,来自algorithmist
#include <vector> using namespace std; /* Finds longest strictly increasing subsequence. O(n log k) algorithm. */ void find_lis(vector<int> &a, vector<int> &b) { vector<int> p(a.size()); int u, v; if (a.empty()) return; b.push_back(0); for (size_t i = 1; i < a.size(); i++) { // If next element a[i] is greater than last element of current longest subsequence a[b.back()], just push it at back of "b" and continue if (a[b.back()] < a[i]) { p[i] = b.back(); b.push_back(i); continue; } // Binary search to find the smallest element referenced by b which is just bigger than a[i] // Note : Binary search is performed on b (and not a). Size of b is always <=k and hence contributes O(log k) to complexity. for (u = 0, v = b.size()-1; u < v;) { int c = (u + v) / 2; if (a[b[c]] < a[i]) u=c+1; else v=c; } // Update b if new value is smaller then previously referenced value if (a[i] < a[b[u]]) { if (u > 0) p[i] = b[u-1]; b[u] = i; } } for (u = b.size(), v = b.back(); u--; v = p[v]) b[u] = v; } /* Example of usage: */ #include <cstdio> int main() { int a[] = { 1, 9, 3, 8, 11, 4, 5, 6, 4, 19, 7, 1, 7 }; vector<int> seq(a, a+sizeof(a)/sizeof(a[0])); // seq : Input Vector vector<int> lis; // lis : Vector containing indexes of longest subsequence find_lis(seq, lis); //Printing actual output for (size_t i = 0; i < lis.size(); i++) printf("%d ", seq[lis[i]]); printf("\n"); return 0; }
2023年9月26日 22:08
Here you will get the NCERT Class 7 Exemplar Problems and Solutions 2024 for All Pdf Chapters, All the Questions are Provided with an easy and Logical Explanation so as to give Students Understanding of the NCERT Exemplar Solutions for 7th 2024 Exemplar Problems.We Suggest you to go Through NCERT 7th Class Exemplar Problems 2024 and solve them before you see their Answers, Practicing these Exemplar Solutions will help you a lot in your School Exam, All Important Exemplar Solutions for Class 7 Given here are as per Latest Exemplar Solutions & Exercises of NCERT, Also these NCERT 7 Class Important Questions.