AYITACM2016省赛第三周 B-递增递减子序列(dp+二分)

寻找最长Wavio序列
本文介绍了一种算法,用于从给定整数序列中找出最长的Wavio子序列。Wavio序列是一种特殊序列,它首先严格递增到达顶点后严格递减。文章通过正序和倒序计算最长递增和递减子序列长度,进而求得最长Wavio序列长度。

Wavio is a sequence of integers. It has some interesting properties.

• Wavio is of odd length i.e. L = 2 ∗ n + 1.
• The first (n + 1) integers of Wavio sequence makes a strictly increasing sequence.
• The last (n + 1) integers of Wavio sequence makes a strictly decreasing sequence.
• No two adjacent integers are same in a Wavio sequence.
For example 1, 2, 3, 4, 5, 4, 3, 2, 0 is an Wavio sequence of length 9. But 1, 2, 3, 4, 5, 4, 3, 2, 2 is
not a valid wavio sequence. In this problem, you will be given a sequence of integers. You have to find
out the length of the longest Wavio sequence which is a subsequence of the given sequence. Consider,
the given sequence as :
1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1.
Here the longest Wavio sequence is : 1 2 3 4 5 4 3 2 1. So, the output will be ‘9’.
Input
The input file contains less than 75 test cases. The description of each test case is given below. Input
is terminated by end of file.
Each set starts with a postive integer, N (1 ≤ N ≤ 10000). In next few lines there will be N
integers.
Output
For each set of input print the length of longest wavio sequence in a line.
Sample Input
10
1 2 3 4 5 4 3 2 1 10
19
1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1
5
1 2 3 4 5
Sample Output
9
9
1

分析:

可以先求出这个序列的最长子序列,并记录每一个数最长子序列。

同理,再倒序求出序列的最长子序列,并记录每一个数最长子序列。

这时候在遍历一遍每个数,结果即为max(min(a[i]的最长增长子序列,a[i]的最长递减子序列)*2-1);理由不言而喻。

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,a[10002],d[10002],w[10002],ad[10002],ad2[10002],sum,len,l2;
int erfen(int q[],int l,int r,int k) //二分法
{
    int m;
    while(l<=r)
    {
        m=(l+r)/2;
        if(q[m]==k) //寻找相同的数据
            return m;
        else if(q[m]>k)
            r=m-1;
        else
            l=m+1;
    }
    return l;    
}
int main()
{
    int we,i,j;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1; i<=n; i++)
            scanf("%d",&a[i]);
        sum=len=ad[1]=1;
        d[len]=a[1];
        for(i=2; i<=n; i++)  //正序找到每个数的最长递增序列长度
        {
            if(a[i]>d[len])
            {
                d[++len]=a[i];   //递增序列
                ad[i]=len;  //递增序列长度
            }
            else
            {
                we=erfen(d,1,len,a[i]);  //二分法,寻找序列中的位置
                d[we]=a[i];
                ad[i]=we;
            }
        }
        l2=1;
        w[l2]=a[n];
        ad2[n]=1;
        for(i=n-1; i>=1; i--)  //寻找最长递减子序列
         {
             if(a[i]>w[l2])
            {
                w[++l2]=a[i];  //递减序列
                ad2[i]=l2;  //递减序列长度
            }
            else
            {
                we=erfen(w,1,l2,a[i]); //二分查找在序列中的位置
                w[we]=a[i];
                ad2[i]=we;
            }
         }
        for(i=1; i<=n; i++)
            sum=max(sum,(min(ad[i],ad2[i])*2-1));//寻找最长的递增递减子序列
        printf("%d\n",sum);
    }
    return 0;
}



LeetCode 题目 491 - 递增子序列 (Incremental Subsequence) 是一道关于算法设计的中等难度题目。这道题要求你在给定整数数组 nums 中找出所有长度大于等于 1 的递增子序列递增子序列是指数组中的一串连续元素,它们按照顺序严格增大。 解决这个问题的一个常见策略是使用动态规划(Dynamic Programming),特别是哈希表或者单调栈(Monotonic Stack)。你可以维护一个栈,每当遍历到一个比栈顶元素大的数字时,就将它推入栈,并更新当前最长递增子序列的长度。同时,如果遇到一个不大于栈顶元素的数字,就从栈顶开始检查是否存在更长的递增子序列。 以下是 C++ 解决此问题的一种简单实现: ```cpp class Solution { public: vector<int> lengthOfLIS(vector<int>& nums) { int n = nums.size(); if (n == 0) return {}; // 使用单调栈存储当前已知的最大子序列 stack<pair<int, int>> stk; stk.push({nums[0], 1}); for (int i = 1; i < n; ++i) { while (!stk.empty() && nums[i] > stk.top().first) { // 如果新数大于栈顶元素,找到一个更长的递增子序列 int len = stk.top().second + 1; ans.push_back(len); stk.pop(); } // 如果新数不大于栈顶元素,尝试从当前位置开始寻找更长子序列 if (!stk.empty()) { stk.top().second = max(stk.top().second, 1); } else { stk.push({nums[i], 1}); } } return ans; } private: vector<int> ans; }; ``` 在这个解决方案中,`ans` 存储所有的递增子序列长度,最后返回这个结果向量即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值