码迷,mamicode.com
首页 > 其他好文 > 详细

LIS问题:二分+DP

时间:2020-02-09 16:27:01      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:back   ==   com   AMM   代码   skiing   ret   error   prim   

实现代码:

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!\n";
            }
        }
        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

标签:back   ==   com   AMM   代码   skiing   ret   error   prim   

原文地址:https://www.cnblogs.com/lzw265/p/12287292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!