码迷,mamicode.com
首页 > 其他好文 > 详细

P4309 [TJOI2013]最长上升子序列

时间:2019-02-06 00:00:26      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:数组   typedef   cpp   type   一个   lowbit   cin   ons   树状数组   

题目

P4309 [TJOI2013]最长上升子序列

做法

最长上升序列的求法肯定是烂大街了

水题是肯定的,确定出序列的位置然后套个树状数组就好了(强制在线的话改成线段树维护前缀最值也行)

所以说这题其实难点在与怎么让代码简洁,见识到一个新的\(STL\)\(rope\)

My complete code

#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
typedef int LL;
const LL maxn=1e6;
__gnu_cxx:: rope<LL> a;
LL n;
LL tree[maxn],ans[maxn];
inline LL Lowbit(LL x){ return x&(-x); }
inline LL Query(LL x){
    LL ret(0);
    for(;x;x-=Lowbit(x)) ret=max(ret,tree[x]);
    return ret;
}
inline void Modify(LL x,LL val){
    for(;x<=n;x+=Lowbit(x))
        tree[x]=max(tree[x],val);
}
int main(){
    cin>>n;
    for(LL i=1;i<=n;++i){
        LL p; cin>>p;
        a.insert(p,i);
    }
    for(LL i=0;i<n;++i){
        LL num=a[i];
        ans[num]=Query(num-1)+1;
        Modify(num,ans[num]);
    }
    for(LL i=1;i<=n;++i){
        ans[i]=max(ans[i],ans[i-1]);
        cout<<ans[i]<<endl;
    }return 0;
} 

P4309 [TJOI2013]最长上升子序列

标签:数组   typedef   cpp   type   一个   lowbit   cin   ons   树状数组   

原文地址:https://www.cnblogs.com/y2823774827y/p/10353312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!