求最长不增子序列、最长单调递减子序列、最长不降子序列、最长单调递增子序列长度

免费午餐问题的引申:

代码如下:

#include 
#include 
using namespace std;

int total = 0;
vector v1;//原始数据集合
vector v2;//最长不增子序列暂时集合
vector v3;//最长单调递减子序列暂时集合
vector v4;//最长不降子序列暂时集合
vector v5;//最长单调递增子序列暂时集合

//二分法求最长不增子序列关键字下标(>=)
int binarySearch1(int key,int lowIndex,int highIndex)
{
    if(lowIndex==highIndex)
    {
        if(lowIndex == 0 && key>v2[0])
        {
            return lowIndex;
        }
        return lowIndex+1;

    }
    int midIndex = (lowIndex + highIndex + 1)/2;
    if(key<=v2[midIndex])
    {
        return binarySearch1(key,midIndex,highIndex);
    }
    else
    {
        return binarySearch1(key,lowIndex,midIndex-1);
    }
}
//二分法求最长单调递减子序列关键字下标(>)
int binarySearch2(int key,int lowIndex,int highIndex)
{
    if(lowIndex==highIndex)
    {
       if(keyv2.size()-1)
        {
            v2.push_back(v1[i]);
        }
        else
        {
            v2[lowIndex] = v1[i];
        }
        //No.2
        lowIndex = binarySearch2(v1[i],0,v3.size()-1);
        if(lowIndex>v3.size()-1)
        {
            v3.push_back(v1[i]);
        }
        else
        {
            v3[lowIndex] = v1[i];
        }
        //No.3
        lowIndex = binarySearch3(v1[i],0,v4.size()-1);
        if(lowIndex>v4.size()-1)
        {
            v4.push_back(v1[i]);
        }
        else
        {
            v4[lowIndex] = v1[i];
        }
        //NO.4
        lowIndex = binarySearch4(v1[i],0,v5.size()-1);
        if(lowIndex>v5.size()-1)
        {
            v5.push_back(v1[i]);
        }
        else
        {
            v5[lowIndex] = v1[i];
        }

    }

    cout<<"最长不增子序列长度:"<>n;
    for(int i=0;i>a;
        v1.push_back(a);
    }
    if(n!=0)
    {
        countTotal();
    }
    return 0;
}


你可能感兴趣的:(Algorithm_LIS,Algorithm_二分)