模板 - 最长上升/下降子序列(新)

模板 - 最长上升/下降子序列(新)

///注意取值的范围,包括最大值和N是否能取到
//需要先定义一个st[]数组,对A
//最长上升子序列 (非严格)
int LUS (int A[],int N) { 
    mmm(st,0x3f);
    rep(i,1,N) { //当需要改严格时,将upper_bound改成lower_bound
        st[upper_bound(st,st+MAXN,A[i])-st] = A[i];
    }
    return lower_bound(st,st+MAXN,INF)-st ;
}

//最长下降子序列 (非严格)
bool cmp (const int &a,const int &b){
    return a > b;
}
int LDS (int A[],int N) {
    mmm(st,-1);  // 或用fill(st,-INF)
    rep(i,1,N) { //当需要改严格时,将upper_bound改成lower_bound
        st[upper_bound(st,st+MAXN,A[i],cmp)-st] = A[i];
    }
    return lower_bound(st,st+MAXN,-1,cmp)-st ;
}

猜你喜欢

转载自blog.csdn.net/qq_41428565/article/details/80195781
今日推荐