"Template" The longest increment sub-sequence
General situation:
1#include <stdio.h>2#include <algorithm>3#include <string.h>4 using namespacestd;5 6 inta[1005],dp[1005],n;7 8 intLIS ()9 {Ten inti,j,ans,m; Onedp[1] =1; AAns =1; - for(i =2; i<=n;i++) - { them =0; - for(j =1; j<i;j++) - { - if(Dp[j]>m && a[j]<A[i]) +m =Dp[j]; - } +Dp[i] = m+1; A if(dp[i]>ans) atAns =Dp[i]; - } - returnans; -}
Two-point optimization:
1#include <stdio.h>2#include <string.h>3#include <algorithm>4 using namespacestd;5 6 inta[40005],dp[40005],n;7 8 intBinintSizeintk)9 {Ten intL =1, r =size; One while(l<=R) A { - intMid = (l+r)/2; - if(k>Dp[mid]) theL = mid+1; - Else -r = mid-1; - } + returnl; - } + A intLIS () at { - inti,j,ans=1; -dp[1] = a[1]; - for(i =2; i<=n; i++) - { - if(a[i]<=dp[1]) inj =1; - Else if(a[i]>Dp[ans]) toj = + +ans; + Else -j =bin (ans,a[i]); theDP[J] =A[i]; * } $ returnans;Panax Notoginseng}
Template maximum increment sub-sequence