动态规划(二分法改进):LIS问题以及应用题 (子序列型)

LIS 问题解决思路,着手dp数组记录下长度为b的单调递增子序列最小结束元素。

Example: array = [2,1,4,3,8,7,5,6,9]

对每个元素array[i]进行单独判断,考虑array[i]可构成最长为多长的局部LIS。

局部的搜索采用二分,以dp数组为域,以array[i]为判定值,找到一个dp中的位置 j 满足:

  • ( dp[j]<array[i] and dp[j+1]>array[i] ) or dp[j]==NULL 

过程:O(n) in visit array, O(logn) in binsearch_dp -> O(nlogn)

  • array = [2,1,4,3,8,7,5,6,9]
  •     dp = [2,0,0,0,0,0,0,0,0]
  •     dp = [1,0,0,0,0,0,0,0,0]
  •     dp = [1,4,0,0,0,0,0,0,0]
  •     dp = [1,3,0,0,0,0,0,0,0]
  •     dp = [1,3,8,0,0,0,0,0,0]
  •    ......
  •     dp = [1,3,5,6,9,0,0,0,0]

可以得到最长长度是5,某一个LIS是 【1,3,5,6,9】

 C++实现代码:

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;
}

应用题:SlalomSkiing

You are a skier participating in a giant slalom. The slalom track is located on a ski slope, goes downhill and is fenced by barriers on both sides. The barriers are perpendicular to the starting line located at the top of the slope. There are N slalom gates on the track. Each gate is placed at a distinct distance from the starting line and from the barrier on the right-hand side (looking downhill).

You start from any place on the starting line, ski down the track passing as many gates as possible, and finish the slalom at the bottom of the slope. Passing a gate means skiing through the position of the gate.

You can ski downhill in either of two directions: to the left or to the right. When you ski to the left, you pass gates of increasing distances from the right barrier, and when you ski to the right, you pass gates of decreasing distances from the right barrier. You want to ski to the left at the beginning.

Unfortunately, changing direction (left to right or vice versa) is exhausting, so you have decided to change direction at most two times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

The arrangement of the gates is given as an array A consisting of N integers, whose elements specify the positions of the gates: gate K (for 0 ≤ K < N) is at a distance of K+1 from the starting line, and at a distance of A[K] from the right barrier.

For example, consider array A such that:

A[0] = 15 A[1] = 13 A[2] = 5 A[3] = 7 A[4] = 4 A[5] = 10 A[6] = 12 A[7] = 8 A[8] = 2 A[9] = 11 A[10] = 6 A[11] = 9 A[12] = 3

 

 

The picture above illustrates the example track with N = 13 gates and a course that passes eight gates. After starting, you ski to the left (from your own perspective). You pass gates 2, 3, 5, 6 and then change direction to the right. After that you pass gates 7, 8 and then change direction to the left. Finally, you pass gates 10, 11 and finish the slalom. There is no possible way of passing more gates using at most two changes of direction.

Write a function:

class Solution { public int solution(int[] A); }

that, given an array A consisting of N integers, describing the positions of the gates on the track, returns the maximum number of gates that you can pass during one ski run.

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

A[0] = 1 A[1] = 5

the function should return 2.

Write an efficient algorithm for the following assumptions:

  • N is an integer within the range [1..100,000];
  • each element of array A is an integer within the range [1..1,000,000,000];
  • the elements of A are all distinct.

Copyright 2009–2020 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

 二重翻转数组,构造单调递增路径,用LIS解题:

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);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尼卡尼卡尼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值