zoukankan      html  css  js  c++  java
  • 导弹拦截

    LIS 用二分可以O(nlogn)

    Dilworth定理,大概就是一个序列的不下降子序列的最小数量等于这个序列的最长上升子序列的长度

    感性理解

    赵宗昌老师讲了向前和向后求最长上升子序列的方法,又理解了动归

    #include<iostream> 
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int maxn=1e5+7;
    int a[maxn],n,f[maxn],l,r,mid;
    int main(){
        while(cin>>a[++n]);n--;
        int ans1=0;
        f[0]=2147483647;
        for(int i=1;i<=n;i++){
            if(f[ans1]>=a[i]){
                f[ans1+1]=a[i];
                ans1++;
            }
            else{
                int l=0;r=ans1;
                while(l<r){
                    mid=(l+r)/2;
                    if(f[mid]>=a[i]) l=mid+1;
                    else r=mid;
                }if(l!=0) f[l]=a[i];
            }
            
        }
        cout<<ans1<<endl;
        memset(f,-1,sizeof(f));
        int ans2=0;
        for(int i=1;i<=n;i++){
            if(f[ans2]<a[i]){
                f[ans2+1]=a[i];
                ans2++;
            }
            else{
                l=0;r=ans2;
                while(l<r){
                    mid=(l+r)/2;
                    if(f[mid]>=a[i]) r=mid;
                    else l=mid+1;
                }
                f[l]=a[i];
            }
        } 
        cout<<ans2<<endl; 
        return 0;
    }     

     不可以相等用upper_bound    可以用lower_bound

    upper_bound是大于的第一个数,lower_bound是大于等于的第一个数

  • 相关阅读:
    算法提高 身份证号码升级
    枚举排列
    排列数
    算法训练 K好数
    算法训练 前缀表达式
    算法训练 区间k大数查询
    最大最小公倍数
    Anagrams问题
    Uiautomator 2.0
    Uiautomator 2.0
  • 原文地址:https://www.cnblogs.com/lcan/p/9409844.html
Copyright © 2011-2022 走看看