其他分享
首页 > 其他分享> > POJ2533 Longest Ordered Subsequence (线性DP)

POJ2533 Longest Ordered Subsequence (线性DP)

作者:互联网

设dp[i]表示以i结尾的最长上升子序列的长度。

dp[i]=max(dp[i],dp[j]+1).

 1 #include  <map>
 2 #include  <set>
 3 #include  <cmath>
 4 #include  <queue>
 5 #include  <cstdio>
 6 #include  <vector>
 7 #include  <climits>
 8 #include  <cstring>
 9 #include  <cstdlib>
10 #include  <iostream>
11 #include  <algorithm>
12 using namespace std;
13 const int maxn=1010;
14 int dp[maxn],a[maxn];
15 
16 int main(){
17     int n,ans=0;
18     scanf("%d",&n);
19     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
20     memset(dp,0,sizeof(dp));
21     for(int i=1;i<=n;i++){
22         dp[i]=1;
23         for(int j=1;j<i;j++)
24             if(a[j]<a[i])
25                 dp[i]=max(dp[i],dp[j]+1);
26         if(dp[i]>ans) ans=dp[i];
27     }
28     printf("%d\n",ans);
29 }

 

标签:Ordered,int,28,Subsequence,maxn,Longest,ans,include,dp
来源: https://www.cnblogs.com/yhxnoerror/p/16386807.html