最長遞增子序列O(n^2)版
阿新 • • 發佈:2019-02-11
#include<iostream>
#include<string.h>
using namespace std;
#define max(a, b)(a>b ? a:b)
int solve(int *num, int n)
{
int *dp = new int[n];
memset(dp, 1, sizeof(dp));
for(int l=0; l<n; l++)
dp[l] = 1;
for(int i=0; i<n; i++)
{
for(int j=0; j<i; j++)
{
if(num[i] > num[j] && dp[i] < dp[j]+1)//dp[i] = max(dp[i], dp[j]+1) num[i]>num[j]條件下
dp[i] = dp[j] + 1;//dp[i]表示0 1 2 ...i之前的比num[i]小的元素的個數
}
}
int ret = 0;
for(i=0; i<n; i++)//找出最大解
ret = max(ret, dp[i]);
delete []dp;
return ret;
}
int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
if(n==0) break;
int *num = new int[n];
for(int i=0; i<n; i++)
scanf("%d", &num[i]);
int ret = solve(num, n);
printf("%d\n", ret);
delete []num;
}
return 0;
}