zoukankan      html  css  js  c++  java
  • 最长递增子序列

    int BiSearch(int len, int w)
    {
        int left = 0, right = len - 1;
        int mid;
        while (left <= right)
        {
            mid = left + (right-left)/2;
            if (bz[mid] > w)
                right = mid - 1;
            else if (bz[mid] < w)
                left = mid + 1;
            else
                return mid;
        }
        return left;
    }
    int LIS(int n) //n为arr数组的长度 从0开始存
    {
        len = 1;
        bz[0] = arr[0];
        int pos = 0;
        for(int i=1; i<n; ++i)
        {
            if(arr[i] > bz[len-1])
            {
                bz[len] = arr[i];
                ++len;
            }
            else
            {
                pos = BiSearch(len, arr[i]);
                bz[pos] = arr[i];
            }
        }
        return len;
    }
    View Code
        int d[];
        int res[];
        int now=1;
        d[1]=res[0];
        int len;
        for(int i=1;i<len;i++)
        {
            if(res[i]>d[now])
            {
            d[++now]=res[i];
            }
            else
            {
            int j=lower_bound(d+1,d+now,res[i])-d;
            d[j]=res[i];
            }
        }
    2

     >为严格递增

    /*Huyyt*/
    #include<bits/stdc++.h>
    #define mem(a,b) memset(a,b,sizeof(a))
    #define pb push_back
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const ll LLmaxn = 2e18;
    const int N = 1e5 + 5;
    inline void read(int &v)
    {
            v = 0;
            char c = 0;
            int p = 1;
            while (c < '0' || c > '9')
            {
                    if (c == '-')
                    {
                            p = -1;
                    }
                    c = getchar();
            }
            while (c >= '0' && c <= '9')
            {
                    v = (v << 3) + (v << 1) + c - '0';
                    c = getchar();
            }
            v *= p;
    }
    int n;
    int num[N];
    int lis[N];
    int nowmaxn[N];
    int aim;
    int pop=1;
    int main()
    {
            int n;
            read(n);
            for(int i=1;i<=n;i++)
            {
                    read(num[i]);
            }
            nowmaxn[1]=num[1];
            lis[1]=1;
            for(int i=2;i<=n;i++)
            {
                    if(num[i]>nowmaxn[pop])
                    {
                            nowmaxn[++pop]=num[i];
                            lis[i]=pop;
                    }
                    else
                    {
                            aim=lower_bound(nowmaxn,nowmaxn+pop,num[i])-nowmaxn;
                            lis[i]=aim;
                            nowmaxn[aim]=num[i];
                    }
            }
            for(int i=1;i<=n;i++)
            {
                    cout<<lis[i]<<" ";
            }
            cout<<endl;
            cout<<pop<<endl;
            return 0;
    }
    View Code
  • 相关阅读:
    css选择器
    HTML标签用法
    pyenv python 版本控制
    Django之路
    Day15-Django
    python+selenium实现登录账户
    requests and BeautifulSoup
    清除MAC 可清除空间
    将python源文件打包成exe文件
    swift的一些东西
  • 原文地址:https://www.cnblogs.com/Aragaki/p/7203297.html
Copyright © 2011-2022 走看看