POJ-2533 longest Ordered subsequence (longest ascending subsequence)

Source: Internet
Author: User

D. Longest ascending sub-sequence

S. Attention is strictly incremented

C.O (NLOGN)

#include <iostream>#include<stdio.h>using namespacestd;Const intmaxn=1005;intA[MAXN],B[MAXN];//B[k] is the smallest end element value in sequence A of all increments of K//find a location using a binary search method to make num>b[i-1] and num<b[i] and use num instead of b[i]intSearch (intNumintLowintHigh ) {    intmid;  while(low<=High ) {Mid= (Low+high)/2; if(Num>=b[mid]) low=mid+1; Elsehigh=mid-1; }    returnLow ;}intDP (intN) {    intI,len,pos; b[1]=a[1]; Len=1;  for(i=2; i<=n;i++){        if(A[i]>b[len]) {//if A[i] is larger than the maximum size of the b[] array, insert it directly into the backlen=len+1; B[len]=A[i]; }        Else{//in the b[] array, find the first position larger than a[i] and let A[i] replace the position .Pos=search (A[i],1, Len); B[pos]=A[i]; }    }    returnLen;}intMain () {intN; inti;  while(~SCANF ("%d",&N)) {         for(i=1; i<=n;++i) {scanf ("%d",&A[i]); } printf ("%d\n", DP (N)); }    return 0;}
View Code

C2. O (n^2)

#include <iostream>#include<stdio.h>using namespacestd;#defineMAXN 1005intA[MAXN];intDP[MAXN];intMain () {intN; inti,j; inttemp; intans;  while(~SCANF ("%d",&N)) {         for(i=1; i<=n;++i) {scanf ("%d",&A[i]); } dp[1]=1;  for(i=2; i<=n;++i) {Temp=0;  for(j=1; j<i;++j) {                if(a[i]>A[j]) {                    if(temp<Dp[j]) {Temp=Dp[j]; }}} Dp[i]=temp+1; } ans=0;  for(i=1; i<=n;++i) {            if(dp[i]>ans) {ans=Dp[i]; }} printf ("%d\n", ans); }    return 0;}
View Code

POJ-2533 longest Ordered subsequence (longest ascending subsequence)

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.