P1049Maximum non-descending subsequence time: 1000ms/Space: 131072kib/java Class Name: main describes the length of the longest non-descending subsequence input format the first behavior N, which represents the number of n
Second row n number of output format longest non-descending subsequence length test sample 1 input
3
1 2 3
Output
3
Note N is less than 5000
For each num <=maxint Test instructions: Chinese test instructions: No descent is >= n^n dp[i] Indicates the length of the longest non-descending subsequence of the number of previous I
1 /******************************2 code by drizzle3 blog:www.cnblogs.com/hsd-/4 ^ ^ ^ ^5 o o6 ******************************/7 //#include <bits/stdc++.h>8#include <iostream>9#include <cstring>Ten#include <cstdio> One#include <map> A#include <algorithm> -#include <cmath> - #definell Long Long the #definePI ACOs (-1.0) - #defineMoD 1000000007 - using namespacestd; - intN; + inta[5005]; - intdp[5005]; + intMain () A { at while(~SCANF ("%d",&N)) { -Memset (DP,0,sizeof(DP)); - for(intI=1; i<=n;i++) - { -scanf"%d",&a[i]); -dp[i]=1; in } - intmaxn=1; to intans=0; + for(intI=1; i<=n;i++) - { themaxn=1; * for(intj=1; j<i;j++) $ {Panax Notoginseng if(a[j]<=a[i]&&dp[j]+1>MAXN) -maxn=dp[j]+1; the } +dp[i]=MAXN; Aans=Max (ANS,MAXN); the } +cout<<ans; - } $ return 0; $}
Nlogn using Upper_bound and the longest ascending subsequence to judge the boundary of different attention
Ans[i] represents the longest non-descending value of the last digit of length I
1 /******************************2 code by drizzle3 blog:www.cnblogs.com/hsd-/4 ^ ^ ^ ^5 o o6 ******************************/7 //#include <bits/stdc++.h>8#include <iostream>9#include <cstring>Ten#include <cstdio> One#include <map> A#include <algorithm> -#include <cmath> - #definell Long Long the #definePI ACOs (-1.0) - #defineMoD 1000000007 - using namespacestd; - intN; + inta[5005]; - intMain () + { A while(~SCANF ("%d",&N)) { atMemset (DP,0,sizeof(DP)); - for(intI=0; i<n;i++) -scanf"%d",&a[i]); - intmaxn=1; - intans[5005]; - inttop=1; inans[1]=a[0]; - for(intI=1; i<n;i++) to { + if(a[i]>=Ans[top]) -ans[++top]=A[i]; the Else * { $ intPos=upper_bound (Ans,ans+top,a[i])-ans;//points to the position of the first element greater than A[i]Panax Notoginsengans[pos]=a[i];//Update - } the } +cout<<top<<Endl;; A } the return 0; +}
TYVJ 1049 Longest non-descending sub-sequence N^2/nlogn