Given a sequence determined increase its longest sequence or the longest sequence length is not reduced or the output sequence
A dynamic programming O (n ^ 2)
1. seek length
First, let's discuss the case of the longest rising sequence, that is a strictly increasing subsequence
If we denote dp [i] in a [i] is the end of the rising length of the sequence sub Then for j (1 <= j <i), if a [j] <a [i], it is clear that:
Code:
#include <bits/stdc++.h> using namespace std; int anss=0,i,n,j,a[10005],dp[10005]; int main() { cin>>n; for(i=0;i<n;i++) { cin>>a[i]; } for(i=0;i<n;i++) { dp[i]=1; for(j=0;j<i;j++) { if(a[j]<a[i]) // If this is not the longest sequence would drop to IF (A [J] <= A [I]) { DP [I] = max (DP [J] + . 1 , DP [I]); } } IF (DP [I]> ANSS) ANSS = DP [I]; } COUT << ANSS << endl; }
2. seek sequence
Just from an array dp forward traversal, find dp [i] == after the anss find dp [i] .... == anss-1 is so you can
#include <bits/stdc++.h> using namespace std; int anss=0,i,n,j,a[10005],dp[10005]; int main() { cin>>n; for(i=0;i<n;i++) { cin>>a[i]; } for(i=0;i<n;i++) { dp[i]=1; for(j=0;j<i;j++) { if(a[j]<a[i]) { dp[i]=max(dp[j]+1,dp[i]); } } if(dp[i]>anss) anss=dp[i]; } cout<<anss<<endl; }
However, solutions generally increase the longest sequence is not unique, the length is unique.
Second, the greedy O (nlogn)
For a rising sequence, it is clear that the end of the element the smaller, the better then other elements in the back, the more likely to become longer.
Therefore, we only need to maintain dp array (although not the dynamic planning), for each of a [i], if a [i] can receive back LIS, it Jieshangqu; otherwise, use a [i] take Updates dp array: greater than or equal to find first one a [i] in the array element dp dp [j], with a [i] to update dp [j]. How to find the first element is greater than equal to it? Only you need to use lower_bound () function can be.
As for the longest sequence does not drop, simply "dp elements found in the first array is greater than equal to a [i] to dp [J]" This step is to find a first element can be greater than. The same will be replaced lower_bound upper_bound ()
1. Code
#include <bits/stdc++.h> using namespace std; int anss=0,i,n,j,a[10005],dp[10005],temp; int main() { cin>>n; for(i=0;i<n;i++) { cin>>a[i]; dp[i]=0x7ffffff; } for(i=0;i<n;i++) { temp=lower_bound(dp,dp+n,a[i])-dp; if(temp+1>anss) { anss++; } dp[temp]=a[i]; } cout<<anss<<endl; }
2. seek sequence
Only needs to go to another array b, the recording position of dp [i], and then traversed forward from the array b, find b [i] == anss after find b [i] == anss-1 to .... and so on
#include <bits/stdc++.h> using namespace std; int anss=0,i,n,j,a[10005],dp[10005],temp,b[10005],c[10005]; int main() { cin>>n; for(i=0;i<n;i++) { cin>>a[i]; dp[i]=0x7ffffff; } for(i=0;i<n;i++) { temp=lower_bound(dp,dp+n,a[i])-dp; if(temp+1>anss) { anss++; } dp[temp]=a[i]; b[i]=temp; } cout<<anss<<endl; temp=anss-1; for(i=n-1;i>=0;i--) { if(b[i]==temp) { c[temp]=a[i]; temp--; } } for(i=0;i<anss;i++) { cout<<c[i]<<' '; } }