Basis of the longest ascending subsequence

Source: Internet
Author: User

Font Size: aaaaaade133
Returns a sequence x [1. n] consisting of n numbers, and finds the length of its longest monotonic ascending subsequence. Find the maximum length of m and a1, a2 ......, Am, making a1 <a2 <... ... <Am and x [a1] <x [a2] <... ... <X [am].
Input
Enter an integer t (t <= 200) to represent the number of test groups. Enter N for each group of data, which indicates N numbers (1 <= N <= 1000 ). enter N positive integers, a1, a2, a3 ..... an (0 <= ai <= 100000 ).
Output
Each group outputs an integer representing the longest length.
Sample Input
171  7  3  5  9  4  8
Sample Output
4



The Code is as follows:#include #define maxn 1005int a[maxn]; int dp[maxn]; int max(int x,inty){ returnx>y?x:y;} int main() { intt,n; scanf("%d",&t); while(t--) { scanf("%d",&n); inti,j; for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=0;i<=n;i++) dp[i]=1; intans=0; for(i=1;i<=n;i++) { for(j=1;j if (a[j] dp[i]=max(dp[i],dp[j]+1); ans=max(dp[i],ans); } printf("%d\n",ans); } return0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.