首页 > 编程语言 > 详细

用树状数组写的最长上升子序列(友好城市),nlogn。

时间:2017-09-09 12:28:31      阅读:876      评论:0      收藏:0      [点我收藏+]
#include<iostream>
#include<algorithm>
#define maxn 100000
#define lb(x) x&-x
using namespace std;
int f[maxn],n,m,k,tot;
struct st{
    int a,b;
}s[maxn];
void update(int x,int val)
{
    while(x<maxn)
    {
        f[x]=max(f[x],val);
        x+=lb(x);
    }
}
int sum(int x)
{
    int ans=0;
    while(x)
    {
        ans=max(ans,f[x]);
        x-=lb(x);
    }
    return ans;
}
bool com(st x,st y)
{
    return x.a<y.a;
}
main(){
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>s[i].a>>s[i].b;
    sort(s+1,s+1+n,com);
    for(int i=1;i<=n;i++)
    {
        k=sum(s[i].b);
        update(s[i].b,k+1);
        tot=max(tot,k+1);
    }
    cout<<tot;
}

 

用树状数组写的最长上升子序列(友好城市),nlogn。

原文:http://www.cnblogs.com/kczno1fans/p/7497524.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!