一般情况:
#include <stdio.h> #include <algorithm> #include <string.h> using namespace std; int a[1005],dp[1005],n; int LIS() { int i,j,ans,m; dp[1] = 1; ans = 1; for(i = 2;i<=n;i++) { m = 0; for(j = 1;j<i;j++) { if(dp[j]>m && a[j]<a[i]) m = dp[j]; } dp[i] = m+1; if(dp[i]>ans) ans = dp[i]; } return ans; }
#include <stdio.h> #include <algorithm> #include <string.h> using namespace std;
int d[1005],p[1005],n;
int LIS() { int i,j,sta,end,mid,l; for(i=2;i<=n;i++) { sta=1; end=l+1; while(sta<end) { mid=(sta+end)/2; if(d[i]<=p[mid]) end=mid; else sta=mid+1; } p[end]=d[i]; if(end==l+1) l++; } return l; }
原文地址:http://blog.csdn.net/jiangx1994/article/details/38013583