Lis Longest ascending subsequence O (nlogn) algorithm

Source: Internet
Author: User
Tags acos

The normal way to find the LIS is to do with DP, time complexity of O (n^2), but the face of some problems when the complexity is a bit high, to learn a NLOGN solution. The main use of two points to find, STL inside the Lower_bound can also.

Upper_bound (i) returns the last position (upper bound) where the element with the key value of I can be inserted
Lower_bound (i) returns the first position of the position where the element with the key value of I can be inserted (lower bound)

Put an O (n^2) code First

1#include <bits/stdc++.h>2 #defineFi first3 #defineSe Second4 #definePB Push_back5 #defineFio Ios::sync_with_stdio (false); Cin.tie (0);6 #definePII pair<int,int>7 #defineVI vector<int>8 #defineVC Vector<char>9 #defineMii map<int,int>Ten #defineSi (a) scanf ("%d", &a) One #defineSS (a) scanf ("%s", &a) A #defineSL (a) scanf ("%i64d", &a); - #defineSLF (a) scanf ("%lf", &a); - #defineCLEAR (A, B) memset (A,b,sizeof (a)) the #definePi ACOs (-1) -  - Const intinf=0x3f3f3f3f; - Const intn=2e5+5; +  -typedefLong Longll; +typedefDoubledb; Atypedef unsignedLong Longull; at using namespacestd; - intDp[n]; - intNum[n]; - intN; -  - voidSolve () in { -     intres=0; to      for(intI=0; i<n;i++) +     { -dp[i]=1; the          for(intj=0; j<i;j++) *         { $             if(num[j]<Num[i])Panax Notoginseng             { -                  Do[I]=max (dp[i],dp[j]+1); the             } +res=Max (res,dp[i]); A         } the     } +cout<<res<<Endl; - } $ intMain () $ { - Fio; -     intN; theCin>>N; -      for(intI=0; i<n;i++)Wuyi     { theCin>>Num[i]; -     } Wu solve (); -}

This is very good understanding, for O (NLOGN) solution I read some blog, the more important thing is to replace this operation, each time to find a more than the last value of the time to join the back, hit the small number than the last face when looking for the first one smaller than its number, replace the back one. (Chinese is not good ...) Just look at the example.)

such as 2 5 3 4 9 7 8 10 It is easy to see that its LIS length is 6, the longest is 2 3 4 7 8 10.

The concrete steps are as follows, 2 enter; 5:2 large, enter; At this time because 3:5 small, looking for, 2:3 small, 3 replace 5, 4:3, 4, 9:4, 9, 7:9,, Forward, 4:7, 7, replace 9, followed by direct access, the LIS sequence 2 3 4 7 8 10.

O (NLOGN) code is specified as follows

1#include <bits/stdc++.h>2 #defineFi first3 #defineSe Second4 #definePB Push_back5 #defineFio Ios::sync_with_stdio (false); Cin.tie (0);6 #definePII pair<int,int>7 #defineVI vector<int>8 #defineVC Vector<char>9 #defineMii map<int,int>Ten #defineSi (a) scanf ("%d", &a) One #defineSS (a) scanf ("%s", &a) A #defineSL (a) scanf ("%i64d", &a); - #defineSLF (a) scanf ("%lf", &a); - #defineCLEAR (A, B) memset (A,b,sizeof (a)) the #definePi ACOs (-1) -  - Const intinf=0x3f3f3f3f; - Const intn=2e5+5; +  -typedefLong Longll; +typedefDoubledb; Atypedef unsignedLong Longull; at using namespacestd; - intAns[n]; - intNum[n]; -  - intMain () - { in Fio; -     intN; toCin>>N; +      for(intI=1; i<=n;i++) -Cin>>Num[i]; theans[1]=num[1]; *     intlen=1; $      for(intI=2; i<=n;i++)Panax Notoginseng     { -         if(num[i]>Ans[len]) theans[++len]=Num[i]; +         Else A         { the             intPos=lower_bound (Ans,ans+len,num[i])-ans; +ans[pos]=Num[i]; -         } $     } $      for(intI=1; i<=len;i++) -     { -cout<<ans[i]<<" "; the     } -cout<<Endl;Wuyi } the  -  Wu /* - 8 About 2 5 3 4 9 7 8 $ */

Lis Longest ascending subsequence O (nlogn) algorithm

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.