最长不下降序列nlogn算法

O(nlogn)的算法关键是它建立了一个数组temp[],temp[i]表示长度为i的不下降序列中结尾元素的最小值,用top表示数组目前的长度,算法完成后top的值即为最长不下降子序列的长度。
设当前的以求出的长度为top,则判断num[i]和temp[top]:
1.如果num[i]>=temp[top],即num[i]大于长度为top的序列中的最后一个元素,这样就可以使序列的长度增加1,即top++,然后现在的temp[top]=num[i];

2.如果num[i]<temp[top],那么就在temp[1]...temp[top]中找到最大的j,使得temp[j]<num[i],然后因为temp[j]<num[i],所以num[i]大于长度为j的序列的最后一个元素,那么就可以更新长度为j+1的序列的最后一个元素,即temp[j+1]=num[i]。

#include <iostream>
#define SIZE 1001
 
using namespace std;
 
int main()
{
    int i, j, n, top, temp;
    int stack[SIZE];
    cin >> n;
 
    top = 0;
    /* 第一个元素可能为0 */
    stack[0] = -1;
    for (i = 0; i < n; i++)
    {
        cin >> temp;
        /* 比栈顶元素大数就入栈 */
        if (temp > stack[top])
        {
            stack[++top] = temp;
        }
        else
        {
            int low = 1, high = top;
            int mid;
            /* 二分检索栈中>=temp的第一个数的下标 */
            while(low <= high)
            {
                mid = (low + high) / 2;
                if (temp > stack[mid])
                {
                    low = mid + 1;
                }
                else
                {
                    high = mid - 1;
                }
            }
            /* 用temp替换 */
            stack[low] = temp;       //这个下标的前一个数即为小于temp的最大的数
        }
    }
 
    /* 最长序列数就是栈的大小 */
    cout << top << endl;
 
    //system("pause");
    return 0;
}

 

 

 


用lower_bound函数实现:
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdlib>

using namespace std;

int main()
{
    int stack[1005];
    int n,top=0,temp;
    cin>>n;
    stack[0]=-1;
    for(int i=0;i<n;i++)
    {
        cin>>temp;
        if(temp>=stack[top])
        stack[++top]=temp;
        else
        {
            int loc=lower_bound(stack,stack+top,temp)-stack;  //返回第一个>=temp的数的下标,则这个数的前一个数就是<temp的下标最大的数
              stack[loc]=temp;
        }
    }
    cout<<top<<endl;
    return 0;
}

 
### 最长下降序列 O(nlogn) 算法实现与解释 #### 一、算法原理 最长下降序列(LDS, Longest Decreasing Subsequence),类似于最长上升子序列,可以通过动态规划加二分查找的方法,在O(n log n)的时间复杂度下解决。核心在于维护一个列表`d`,其中存储着可能成为最终LDS一部分的最小结尾元素。每当遇到一个新的数时,如果它小于`d`中的最后一个元素,则更新`d`;否则通过二分查找找到其应在位置并替换之。 #### 二、具体步骤说明 - 初始化一个空的结果数组 `f` 和变量 `len` 表示当前已知的最大长度。 - 遍历输入序列中的每一个数字: - 使用C++标准库函数`lower_bound()`寻找第一个大于等于该数字的位置。 - 如果此位置位于现有记录之外,则扩展结果集;反之则用新数值替代旧值以保持潜在解空间最优性。 上述过程确保了每次迭代都能维持住“尽可能短”的递减路径特性,从而使得最后得到的答案既满足条件又具有最大长度[^1]。 #### 三、代码实例 下面是采用 C++ 编写的完整程序: ```cpp #include <iostream> #include <vector> #include<algorithm> using namespace std; int main(){ int n; cin >> n; vector<int> nums(n+1), f(n+1); for(int i = 1; i <= n; ++i){ cin >> nums[i]; } int len = 0; f[++len] = nums[1]; for (int i = 2; i <= n; ++i) { // 寻找nums[i]应该放置的位置 auto pos = upper_bound(f + 1, f + len + 1, nums[i], greater<int>()) - f; if (pos == len + 1) { f[++len] = nums[i]; } else { f[pos] = nums[i]; } } cout << "Length of LDS is: " << len << "\n\n"; } ``` 这段代码实现了对给定整数序列解其最长严格递减子序列的功能,并输出对应的长度。注意这里使用了`upper_bound`配合自定义比较器`greater<int>()`来适应于寻找降序排列下的插入点[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值