题目&思路:
代码:
import java.util.Scanner;
public class LongestIncreasingSubsequence {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int A[] = new int[n+1];
int L[] = new int[n+1];
int P[] = new int[n+1];
for (int i=1;i<=n;i++){
A[i] = cin.nextInt();
}
//----解法1,复杂度O(n^2)
/* LIS(A, L, P, n);
int max_val=-1;
for (int i=1;i<=n;i++){
if(max_val<L[i]){
max_val = L[i];
}
}
System.out.println(max_val);*/
//----解法2,复杂度O(n*log(n))
advancedLIS(A, L, n);
int len = 0;
for (int i=1;i<=n;i++){
if (L[i]!=0)len++;
}
System.out.println(len);
}
public static void LIS(int A[], int L[], int P[], int n){
L[0] = 0;
A[0] = 0;
P[0] = -1;
for (int i=1;i<=n;i++){
int k =0;
for (int j=0;j<=i-1;j++){
if(A[j]<A[i] && L[j]>L[k]){
k = j;
}
}
L[i] = L[k] + 1;
P[i] = k;
}
}
public static void advancedLIS(int A[], int L[], int n){
L[1] = A[1];
int length = 1;
for (int i=2;i<=n;i++){
if (L[length]<A[i]){
length++;
L[length] = A[i];
}
else {
for (int j=1;j<=length;j++){
if(L[j]>=A[i]){
L[j] = A[i];
break;
}
}
}
}
}
}
输入:
5
5
1
3
2
4
输出:
3