最长上升序列和

题目网址: http://ac.jobdu.com/problem.php?pid=1480
题目描述:

一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的。对于给定的一个序列(a1, a2, ...,aN),我们可以得到一些上升的子序列(ai1, ai2, ..., aiK),这里1 <= i1 < i2 < ... < iK <= N。比如,对于序列(1, 7, 3, 5, 9, 4, 8),有它的一些上升子序列,如(1, 7), (3, 4, 8)等等。这些子序列中序列和最大为18,为子序列(1, 3, 5, 9)的和.

你的任务,就是对于给定的序列,求出最大上升子序列和。注意,最长的上升子序列的和不一定是最大的,比如序列(100, 1, 2, 3)的最大上升子序列和为100,而最长上升子序列为(1, 2, 3)。

输入:

输入包含多组测试数据。
每组测试数据由两行组成。第一行是序列的长度N (1 <= N <= 1000)。第二行给出序列中的N个整数,这些整数的取值范围都在0到10000(可能重复)。

输出:

对于每组测试数据,输出其最大上升子序列和。

样例输入:
7
1 7 3 5 9 4 8
样例输出:
18
来源:
2012年北京大学计算机研究生机试真题

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
int a[1005];
int sum[1005];

int main()
{
    int n,s,f;
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
        cin>>a[i];
        sum[0]=0;
        sum[1]=a[1];
        s=sum[1];
        for(int i=2;i<=n;i++)
        {
            int maxn=0;
            for(int j=1;j<=i-1;j++)
            {
                if(a[j]<a[i]&&sum[j]>maxn)
                {
                    maxn=sum[j];
                }
            }
            sum[i]=maxn+a[i];
            if(sum[i]>s) s=sum[i];
        }
        cout<<s<<endl;
    }
    return 0;
}

时间: 2024-08-28 11:54:44

最长上升序列和的相关文章

最长增长序列的长度(LIS)

我最早的思路是反过来遍历,结果总是不对.因为老是觉得动态规划就是列递归,所以一心琢磨着怎样递归,导致一直不对. 正确的思路应该是什么呢?应该是尝试用符号表示其最优的状态和其子状态,然后找出状态之间转移的方法.最后实现这个方法. 最长增长序列的最优状态是什么样子呢?既然是最长,肯定是要容纳尽可能多的元素,怎样容纳尽可能多的元素呢?那就是较小值尽可能小. 那么遍历到的新元素就存在两种情况: 1. 新元素比已知当前 LIS  中的每个元素都要大,那毫无疑问应该加入 LIS. 2. 新元素比当前 LIS

zoj1986 Bridging Signals (dp,最长递增序列,LIS)

A - Bridging Signals Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Status Practice ZOJ 1986 Description 'Oh no, they've done it again', cries the chief designer at the Waferland chip factory. Once more the routing designer

uva103(最长递增序列,dag上的最长路)

题目的意思是给定k个盒子,每个盒子的维度有n dimension 问最多有多少个盒子能够依次嵌套 但是这个嵌套的规则有点特殊,两个盒子,D = (d1,d2,...dn) ,E = (e1,e2...en) 只要盒子D的任意全排列,小于盒子E,那么就说明 盒子D能放入盒子E中,其实就是将两个盒子的维度排序,如果前一个盒子的维度依次小于后一个盒子,那么就说明前一个盒子能放入后一个盒子中 这个题目能够转化为最长递增子序列. 首先将盒子的维度从小到大排序,然后将k个盒子,按照排序后的第一维度从小到大排

[LeetCode] Longest Consecutive Sequence 求最长连续序列

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run i

矩形网格中寻找最长递增序列

在矩形网格中寻找最长的递增序列 比如如下网格 97,47,56,36 35,57,41,13 89,36,98,75 25,45,26,17 结果要求输出 17, 26, 36, 41, 47, 56, 57, 97 基本想法就是对图中的每一个点都当作起始点试一编 将序列最长的保存起来 最后输出 代码如下 使用java编写 import java.util.ArrayList; public class 最长递增序列 { static int[][] rect={ {97,47,56,36},

POJ 2533 Longest Ordered Subsequence 最长递增序列

Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, seque

[LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from p

算法提高 最长字符序列

最长字符序列 问题描述 设x(i), y(i), z(i)表示单个字符,则X={x(1)x(2)--x(m)},Y={y(1)y(2)--y(n)},Z={z(1)z(2)--z(k)},我们称其为字符序列,其中m,n和k分别是字符序列X,Y,Z的长度,括号()中的数字被称作字符序列的下标. 如果存在一个严格递增而且长度大于0的下标序列{i1,i2--ik},使得对所有的j=1,2,--k,有x(ij)=z(j),那么我们称Z是X的字符子序列.而且,如果Z既是X的字符子序列又是Y的字符子序列,那

GEEK编程练习— —最长连续序列

题目 给定一个无序的整数数组,返回最长连续序列的长度.要求时间复杂度为O(n). 输入 [100, 4, 200, 1, 3, 2, 0, -1] 输出 6 分析 因为要求时间负责度为O(n),所以不能先排序再查找.所以想到查询最快的hash表,记录每个元素是否使用,对每个元素,往左右扩张,直到不连续为止. 代码 #include <iostream> #include <unordered_map> #include <algorithm> using namespa

LIS 最长上升序列

#include <iostream> #include <stdlib.h> using namespace std; #define MAX 10000 int num[MAX], n; /*********************************************************************************************** 经典的O(n^2)的动态规划算法,设num[i]表示序列中的第i个数, dp[i]表示从1到i这一段