LIS问题:二分+DP

Posted lzw265

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LIS问题:二分+DP相关的知识,希望对你有一定的参考价值。

实现代码:

int Longest_increasing_subseq(vector<int> &array){
    vector<int> dp(array.size());
    dp[0]=-1;
    int left,right,mid;
    int count=0;
    for(int i=0;i<array.size();++i){
        left=0;
        right=count;
        while(left<=right){
            mid = (left+right)/2;
            if(array[i]<dp[mid]){
                right=mid-1;
            }else if(array[i]>dp[mid]){
                left=mid+1;
            }else{
                cout << "case Error happened!
";
            }
        }
        if(dp[left]==0){
            dp[left]=array[i];
            ++count;
        }
        else{
            dp[left]=min(dp[left],array[i]);
        }
    }
    return count;
}

应用于解决问题:https://app.codility.com/programmers/lessons/90-tasks_from_indeed_prime_2015_challenge/slalom_skiing/

SlalomSkiing:

int solution(vector<int> &A){
    vector<int> B;
    int mmax=(*max_element(A.begin(),A.end()))+1;
    // overturn A to two states, and extend to B as input_Array
    for (int i=0;i < A.size();++i){
        // avoid extend_elements destory A charactor of Increasing subseq in local
        B.push_back(mmax*2+A[i]);
        B.push_back(mmax*2-A[i]);
        B.push_back(A[i]);
    }
    return Longest_increasing_subseq(B);
}

 

以上是关于LIS问题:二分+DP的主要内容,如果未能解决你的问题,请参考以下文章

POJ 3903 Stock Exchange LIS

LIS 树状数组优化

HDU 4352:XHXJ's LIS

二分求LIS并打印结果

大数据LIS (贪心+二分优化/树状数组优化)

最长递增子序列的个数--了解两种实现LIS算法的区别