BZOJ1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛

题意
给定长度为n的序列,求最长上升子序列
复杂度
O(nlogn)
题解
网上有很多关于最长上升子序列nlogn的求法,我这里不在过多叙述。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,a[5001],last[5001],ans;
int main()
{
    memset(last,127,sizeof(last));
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    last[0]=0;
    for(int i=1;i<=n;i++)
    {
        int tmp=lower_bound(last,last+n,a[i])-last;
        last[tmp]=min(last[tmp],a[i]);
        ans=max(ans,tmp);
    }
    printf("%d",ans);
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容