[POJ2533]Longest Ordered Subsequence<dp>
题目链接:http://poj.org/problem?id=2533
描述:
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.
翻译:
找一串数字最长上升子序列的数字个数。(手动狗头)
没有难度的一个dp题,范围不算大,最普通的方法就可以过了
想优化看到了其他博主说的二分,不过我没有用
等复习到二分再来试试吧
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#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; }