题目连接: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; }