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的主要内容,如果未能解决你的问题,请参考以下文章