题目描述:
给出长度为N的数组,找出这个数组的最长递增子序列。(递增子序列是指,子序列的元素是递增的)
例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10。
输出
输出最长递增子序列的长度。
输入示例
8
5
1
6
8
2
4
5
10
输出示例
5
import java.util.Scanner;
public class LIS {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
int[] f = new int[n];
f[0] = a[0];
int len = 1;
for (int i = 1; i < n; i++) {
int left = 0;
int right = len - 1;
while (left <= right) {
int mid = (left + right) >> 1;
int val = f[mid];
if (val < a[i])
left = mid + 1;
else
right = mid - 1;
}
f[left] = a[i];
if (left == len)
len++;
}
for (int aa : f) {
System.out.print(aa + " ");
}
System.out.println(len);
}
}