題目連接:http://poj.org/problem?id=2533ios
描述:ide
A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.優化
翻譯:spa
找一串數字最長上升子序列的數字個數。(手動狗頭)翻譯
沒有難度的一個dp題,範圍不算大,最普通的方法就能夠過了code
想優化看到了其餘博主說的二分,不過我沒有用blog
等複習到二分再來試試吧get
#include<cstdio> #include<iostream> #include<cmath> #include<algorithm> using namespace std; int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,a[10005],dp[10005],ans; int main(){ n=read(); for(int i=1;i<=n;i++) a[i]=read(); for(int i=2;i<=n;i++) for(int j=1;j<i;j++) if(a[i]>a[j])dp[i]=max(dp[i],dp[j]+1); for(int i=1;i<=n;i++) ans=max(ans,dp[i]); cout<<ans+1; return 0; }