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)