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

博客探讨了如何求解最长不增子序列、最长单调递减子序列、最长不降子序列和最长单调递增子序列的长度。内容涉及算法实现和问题分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

免费午餐问题的引申:

代码如下:

#include <iostream>
#include <vector>
using namespace std;

int total = 0;
vector<int> v1;//原始数据集合
vector<int> v2;//最长不增子序列暂时集合
vector<int> v3;//最长单调递减子序列暂时集合
vector<int> v4;//最长不降子序列暂时集合
vector<int> 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(key<v3[lowIndex])
        {
            return lowIndex+1;
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值