标签:最长上升子串
#include <iostream> #include <algorithm> using namespace std; const int MAX = 100; int dp[MAX]; int a[MAX]; int n; const int INF = 1<<30; void slove(){ //lower_bound:表示返回指向a[i]的最小指针位置,不存在就返回第一个不小于a[i]的元素位置 fill(dp,dp+n,INF); for(int i=0;i<n;i++) *lower_bound(dp,dp+n,a[i]) = a[i]; cout<<(lower_bound(dp,dp+n,INF)-dp)<<endl; } int main() { cin>>n; for(int i=0;i<n;i++) cin>>a[i]; slove(); return 0; }
#include <iostream> using namespace std; const int MAX = 100; int dp[MAX]; int a[MAX]; int n; int search(int num,int low,int high){ int mid; while(low<=high){ mid = low+((high-low)>>1);//(low+high)/2; if(num>=dp[mid]) low = mid+1; else high = mid-1; } return low; } int DP(){ int len=0,pos; dp[0]=a[0]; for(int i=1;i<n;i++){ if(a[i]>=dp[len]){ len = len+1; dp[len] = a[i]; }else{ pos = search(a[i],0,len); dp[pos] = a[i]; } } cout<<len+1<<endl; return len+1; } int main() { cin>>n; for(int i=0;i<n;i++) cin>>a[i]; DP(); return 0; }同样的实现,代码相对复杂。
标签:最长上升子串
原文地址:http://blog.csdn.net/xd_122/article/details/40655731