最长递增子序列,动态规划解法:
设 表示的最长递增子序列的长度。
#include <stdio.h>
#include <string.h>
int LIS(int a[], int n) {
int longest[50005];
for(int i = 0; i < n; i++)
longest[i] = 1;
for(int i = 1; i < n; i++)
for(int j = 0; j < i; j++)
if(a[j] < a[i] && longest[j] + 1 > longest[i]) //严格递增
longest[i] = longest[j] + 1;
int maxLen = 1;
for(int i = 0; i < n; i++)
if(longest[i] > maxLen)
maxLen = longest[i];
return maxLen;
}
int main() {
int n, a[50005];
while(~scanf("%d", &n)) {
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
int max = LIS(a, n);
printf("%d\n", max);
}
return 0;
}