最长升序子序列

Description

Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

Output

For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

Sample Input

2

5 6 -1 5 4 -7

7 0 6 -1 1 -6 7 -5

Sample Output

Case 1: 14 1 4

Case 2: 7 1 6

思路:

用二分法(避免超时)找最小的数放在子序列里面,比如最开始的序列是 2 3 4,但是后面又发现了一个1,就把1代替之前的2,以免后面出现更长的子序列

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 #define  maxn 100000+10
 5 int al[maxn], cl[maxn];
 6 int main()
 7 {
 8     int n,len,left,right,mid;
 9     while (scanf("%d",&n)!= EOF){
10         for (int i = 0; i < n; i++)
11             scanf("%d", &al[i]);
12         len = 0, cl[0] = -1;
13         for (int i = 0; i < n; i++)
14         {
15             if (al[i]>cl[len])
16                 cl[++len] = al[i];    //把大的数放进去
17             else
18             {
19                 left = 1, right = len;    //二分法找后面小的数放在这个子序列的前面,以免有更长的子序列
20
21                 while (left <= right)
22                 {
23                     mid = (left + right) / 2;
24                     if (al[i]>cl[mid])
25                         left = mid + 1;
26                     else
27                        right = mid - 1;
28                 }
29                 cl[left] = al[i];
30             }
31         }
32         printf("%d\n", len);
33     }
34
35     return 0;
36 }
时间: 2024-08-04 02:10:03

最长升序子序列的相关文章

java最长升序子序列

最长升序子序列是最长公共子序列的变形. 只要将字符串升序排序后与原字符串求最长公共子序列即可. 以下提供一个工具类可以传入任何形式的数组.(添加新类型的数组时构造方法要自己加). package com.leejuen.string; import java.lang.reflect.Array; import java.util.Arrays; public class LCS { private Integer len; private Object str1; private Object

(hdu step 3.2.4)FatMouse&#39;s Speed(在第一关键字升序的情况下,根据第二关键字来求最长下降子序列)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1034 Accepted Submission(s): 526   Proble

数组中最长的升序子序列(动态规划问题)

The longest Increasing Subsequence (LIS)  给定一个序列,找到这个序列的一个最长的子序列,使得子序列的所有元素是升序的,且元素之间的相对位置不变(元素可以在原数组中不相邻,但是相对位置不变) 比如, LIS for { 10, 22, 9, 33, 21, 50, 41, 60, 80 } 是 6,LIS 是 {10, 22, 33, 50, 60, 80}. 分析 arr[0-n-1]是输入,然后L(i)是到元素i为止 LIS 的长度,也就是arr[i]

LIS:最长上升子序列

学习动态规划问题(DP问题)中,其中有一个知识点叫最长上升子序列(longest  increasing subsequence),也可以叫最长非降序子序列,简称LIS.简单说一下自己的心得.(大神可无视..) 我们都知道,动态规划的一个特点就是当前解可以由上一个阶段的解推出, 由此,把我们要求的问题简化成一个更小的子问题.子问题具有相同的求解方式,只不过是规模小了而已.最长上升子序列就符合这一特性.我们要求n个数的最长上升子序列,可以求前n-1个数的最长上升子序列,再跟第n个数进行判断.求前n

HDU 1257 最少拦截系统【最长上升子序列】

解题思路:可以转化为求最长上升子序列来做,还是可以用an与按升序排列后的an求LCS来做,为防止超时,用滚动数组优化一下就可以了. 最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 20625    Accepted Submission(s): 8174 Problem Description 某国为了防御敌国的导弹袭击,

HDU 1160 FatMouse&#39;s Speed 动态规划 记录路径的最长上升子序列变形

题目大意:输入数据直到文件结束,每行两个数据 体重M 和 速度V,将其排列得到一个序列,要求为:体重越大 速度越低(相等则不符合条件).求这种序列最长的长度,并输出路径.答案不唯一,输出任意一种就好了. 题目思路:这是个最长上升子序列的问题,我们按W的升序进行排序,若W相等则按V的降序排序.用Pre[]记录当前点的前驱节点,Last记录序列最后一个点,maxn记录最长长度,完成动规后可根据Last和Pre[]输出路径. #include<cstdio> #include<stdio.h&

51nod 1134 最长递增子序列 (O(nlogn)算法)

1134 最长递增子序列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10. Input 第1行:1个数N,N为序列的长度(2 <= N <= 50000) 第2 - N + 1行:每行1个数,对应序列的元素(-10^9 <= S[i] <= 10^9) Output 输

shuoj1936-D序列—最长上升子序列

Description 已知两个长度为N的数组A和B.下标从0标号至N-1. 如今定义一种D序列 (如果长度为L).这样的序列满足下列条件: 1. 0 <= D[i] <= N-1 2. A[D[i]] < A[D[i+1]]  (0 <= i < L-1) 3. B[D[i]] > B[D[i+1]]  (0 <= i < L-1) 求满足条件的D序列的最大长度. (事实上这样的序列叫做D序列的原因是:这道题是D题) Input 多组数据,每组数据第一行是

动态规划——E (LIS())最长上升子序列

E - LIS Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description The world financial crisis is quite a subject. Some people are more relaxed while others are quite anxious. John is one of them. He is very