TYVJ 1049 Longest non-descending sub-sequence N^2/nlogn

Source: Internet
Author: User
Tags acos cmath

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

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.