给出长度为N的数组,找出这个数组的最长递增子序列。(递增子序列是指,子序列的元素是递增的)
例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10。
输入
第1行:1个数N,N为序列的长度(2 <= N <= 50000) 第2 - N + 1行:每行1个数,对应序列的元素(-10^9 <= S[i] <= 10^9)
输出
输出最长递增子序列的长度。
输入样例
8 5 1 6 8 2 4 5 10
输出样例
5
解:
1 #include<stdio.h> 2 #include<string.h> 3 4 int a[50005]; 5 6 int main() 7 { 8 int n; 9 while (scanf_s("%d", &n) != EOF) 10 { 11 int temp, ans = 1; 12 scanf_s("%d", &a[0]); 13 for (int i = 1; i < n; i++) 14 { 15 scanf_s("%d", &temp); 16 for (int j = 0; j < ans; j++) 17 { 18 if (temp < a[j]) 19 { 20 a[j] = temp; 21 break; 22 } 23 else if (j == ans - 1) 24 { 25 a[ans] = temp; 26 ans++; 27 break; 28 } 29 } 30 31 } 32 printf("%d", ans); 33 34 } 35 }