"Dynamic planning + binary search" poj2533&poj1631 longest ascending subsequence (LIS)

Source: Internet
Author: User

POJ2533 bare lis, Time complexity O (n^2)

1#include <iostream>2#include <cstdio>3 using namespacestd;4 Const intmaxn= ++5;5 intA[MAXN];6 intDP[MAXN];7 intN,ans;8 9 intMain ()Ten { Onescanf"%d",&n); A      for(intI=0; i<n;i++) -     { -scanf"%d",&a[i]); thedp[i]=1; -     } -ans=-1; -      for(intI=1; i<n;i++) +     { -          for(intj=0; j<i;j++) +             if(A[j]<a[i] && dp[j]+1>Dp[i]) A             { atdp[i]=dp[j]+1; -             } -         if(dp[i]>ans) -             { -ans=Dp[i]; -             } in     } -cout<<ans<<Endl; to     return 0; +}

POJ1631

Two lines I and J do not cross the precondition is a[i]<a[j], that is, ascending sub-sequence. Using binary search +lis, the time complexity is O (n^2), the specific explanation is described in the Challenge Program design Competition 2.3 recorded results in the use of "dynamic planning" P65

1#include <iostream>2#include <cstdio>3 using namespacestd;4 Const intmaxn=40000+5;5 Const intinf=1000000+5;6 intA[MAXN];7 intDP[MAXN];//Dp[i] Indicates the minimum value of the lowest element of the ascending subsequence of the length of i+18 intN,m,ans,l,r;9 Ten intSearchintk) One { A     intUl=l,ur=R; -      while(ur-ul>1) -     { the         intMid= (Ur+ul)/2; -         if(dp[mid]>=k) ur=mid; -             ElseUl=mid; -     } +     returnur; - } +  A intMain () at { -scanf"%d",&m); -      for(intKase=0; kase<m;kase++) -     { -scanf"%d",&n); -          for(intI=0; i<n;i++) in         { -scanf"%d",&a[i]); todp[i]=INF; +         } -l=-1; theR=0; *          for(intI=0; i<n;i++) $         {Panax Notoginseng             intpos=search (a[i]); -dp[pos]=A[i]; the             if(pos==r) r++; +         } Acout<<r<<Endl; the     } +     return 0;  -}

Dynamic planning + binary lookup poj2533&poj1631 longest ascending subsequence (LIS)

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.