求一个序列最大的上升子序列的和

本文介绍了一种算法,用于解决寻找一组数值中最大上升子序列的问题,并通过具体实例展示了如何实现这一算法。该算法首先初始化一个存储最大子序列和的数组,然后遍历输入的数值,对于每个数值,它都会检查所有之前的数值来确定当前的最大子序列和。

Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.



The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.
Your task is to output the maximum value according to the given chessmen list.

Input

Input contains multiple test cases. Each test case is described in a line as follow:
N value_1 value_2 …value_N
It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
A test case starting with 0 terminates the input and this test case is not to be processed.

Output

For each case, print the maximum according to rules, and one line one case.

Sample Input

3 1 3 2
4 1 2 3 4
4 3 3 2 1
0

Sample Output

4
10
3

求这些数的 最大的上升子序列

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 1005;
int sum[maxn];
int data[maxn];
int n;
int LIS_SUM()
{
    int i;
    int j;
    int maxSum = -9999;
    memset(sum,0,sizeof(sum));
    for(i = 1 ; i <= n ; ++i)
    {
        int temp = 0;
        for(j = 1 ; j < i ; ++j)
        {
            if(data[j] < data[i])
            {
                if(temp < sum[j])
                {
                    temp = sum[j];
                }
            }
        }
        sum[i] = data[i] + temp;
        if(maxSum < sum[i])
        {
            maxSum = sum[i];
        }
    }
    return maxSum;
}
int main()
{
    while(scanf("%d",&n)!=EOF,n)
    {
        int i;
        for(i = 1 ; i <= n ; ++i)
        {
            scanf("%d",&data[i]);
        }
        printf("%d\n",LIS_SUM());
    }

    return 0;
}

 

### 头歌平台上的最长上升子序列算法实现 在头歌平台上实现计算最长上升子序列(Longest Increasing Subsequence, LIS)问题,可以采用动态规划方法来完成。以下是详细的说明以及代码实现。 #### 动态规划的核心思路 为了找到给定序列中的最长上升子序列长度,可以通过构建一个辅助数组 `dp` 来记录以每个位置结束的最长上升子序列的长度。具体来说,对于每一个元素 `arr[i]`,我们需要遍历它之前的所有元素 `arr[j] (j < i)`,如果满足条件 `arr[j] < arr[i]`,则更新当前状态为 `dp[i] = max(dp[i], dp[j] + 1)`[^1]。 最终的结果将是整个 `dp` 数组的最大值,即表示原序列中可能存在的最长上升子序列的长度。 #### C++代码实现 下面是一个基于上述逻辑的具体C++程序: ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int lengthOfLIS(vector<int>& nums) { if(nums.empty()) return 0; vector<int> dp(nums.size(), 1); int maxLength = 1; for(int i=1;i<nums.size();i++) { for(int j=0;j<i;j++) { if(nums[j]<nums[i]) { dp[i]=max(dp[i], dp[j]+1); } } maxLength=max(maxLength, dp[i]); } return maxLength; } int main(){ vector<int> sequence = {10,9,2,5,3,7,101,18}; cout << "The Length of Longest Increasing Subsequence is: " << lengthOfLIS(sequence) << endl; } ``` 此段代码定义了一个函数 `lengthOfLIS()`,用于接收输入整数向量并返回其对应的最长递增子序列的长度。通过双重循环结构实现了动态规划的状态转移方程,并利用标准库 `<algorithm>` 中的功能简化了一些操作过程[^2]。 #### 时间复杂度分析 该解决方案的时间复杂度主要由两层嵌套循环决定,因此总体时间复杂度为 O(n²),其中 n 是原始序列的大小。虽然存在更高效的二分查找优化版本能够达到O(n log n) 的性能表现,但对于初学者或者教学目的而言,这种基础形式更容易理解掌握[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值