Dynamic Programming rise longest sequence

A given sequence length, the longest seek its rising sequence: The meaning of the questions

Topic link: https: //ac.nowcoder.com/acm/problem/26156

Input: n representative length, then a string

Analysis: with dp [i] indicates the length of the end of the minimum rises sequence of elements i + 1 (beginning initialized to INF)

Approach is readily apparent to two cycles, the first cycle of length i do cycle and the second cycle of layer j, for each AJ, if i = 0 (i.e. length 1), or dp [i-1 ] <when ​​aj, there dp [i] = min (dp [i], aj), the time complexity of this approach is O (N ^ 2)

But in addition to the array dp INF, the other is monotonically increasing, so we can optimize the function with lower_bound ()

lower_bound () function returns val greater than or equal to the first element position, returns a pointer

The specific process can see the code

#include <bits / STDC ++ H.>
 the using  namespace STD; 
typedef Long  Long LL;
 const  int INF = 0x3f3f3f3f ; // this number is 1e9 orders of magnitude, and may be used memset function 
const  int MAXN = 5E4 + . 7 ;
 const  Double PI = ACOS (- . 1 );
 const  int MOD = + 1E9 . 7 ;
 int DP [MAXN], A [MAXN]; 
inline LL Read () { 
    LL X = 0 , = tmp . 1 ;
     char CH = getchar ();
     the while (!isdigit(ch)){
        if(ch=='-') tmp=-1;
        ch=getchar();
    }
    while(isdigit(ch)){
        x=(x<<3)+(x<<1)+ch-48;
        ch=getchar();
    }
    return tmp*x;
}

int main(){
    int n;cin>>n;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    fill(dp,dp+maxn,inf);
    for(int i=1;i<=n;i++){
        *lower_bound(dp,dp+n,a[i])=a[i];
    }
    cout<<lower_bound(dp,dp+n,inf)-dp<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/qingjiuling/p/10977855.html