Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 197869    Accepted Submission(s): 46229

Problem 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

对于一个数,决策只有两种,要么让他跟前面的并在一起,要么前面的扔掉,从这个数开始取。

用dp结构体来表示,比较直观

若把前面的一段与A[i]合并,则当前点的l为dp[i-1].l保持不变,r=dp[i-1].r+1,因为此时A[i]算了进去;

若直接从A[i],那么令dp[i].l=dp[i].r=i即可,dp[i].val=A[i]。

最后找到一个最大的dp[i].val即可

代码:

#include <stdio.h>
const int N = 100010;
struct info
{
int val;
int l, r;
};
info dp[N];
int arr[N]; int main(void)
{
int tcase, n, i;
scanf("%d", &tcase);
for (int q = 1; q <= tcase; ++q)
{
scanf("%d", &n);
for (i = 1; i <= n; ++i)
scanf("%d", arr + i);
dp[1].val = arr[1];
dp[1].l = 1;
dp[1].r = 1;
for (i = 2; i <= n; ++i)
{
int a = dp[i - 1].val + arr[i];
int b = arr[i];
if (a >= b)
{
dp[i].val = a;
dp[i].l = dp[i - 1].l;
dp[i].r = dp[i - 1].r + 1;
}
else
{
dp[i].l = dp[i].r = i;
dp[i].val = arr[i];
}
}
int indx = 1;
for (i = 1; i <= n; ++i)
if (dp[i].val > dp[indx].val)
indx = i;
printf("Case %d:\n%d %d %d\n%s", q, dp[indx].val, dp[indx].l, dp[indx].r, q != tcase ? "\n" : "");
}
return 0;
}

HDU——1003Max Sum(子序列最大和)的更多相关文章

  1. HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)

    C - 最大连续子序列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  2. 【ToReadList】六种姿势拿下连续子序列最大和问题,附伪代码(以HDU 1003 1231为例)(转载)

    问题描述:       连续子序列最大和,其实就是求一个序列中连续的子序列中元素和最大的那个. 比如例如给定序列: { -2, 11, -4, 13, -5, -2 } 其最大连续子序列为{ 11, ...

  3. HDU-1231 简单dp,连续子序列最大和,水

    1.HDU-1231 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=1231 3.总结:水 题意:连续子序列最大和 #include<iostre ...

  4. hdu1087 Super Jumping! Jumping! Jumping!---基础DP---递增子序列最大和

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1087 题目大意: 求递增子序列最大和 思路: 直接dp就可以求解,dp[i]表示以第i位结尾的递增子 ...

  5. 连续子序列最大和的O(NlogN)算法

    对于一个数组,例如:int[] a = {4,-3,5,-2,-1,2,6,-2}找出一个连续子序列,对于任意的i和j,使得a[i]+a[i+1]+a[i+2]+.......+a[j]他的和是所有子 ...

  6. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  7. HDU 1069 Monkey and Banana (动态规划、上升子序列最大和)

    Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. HDU 1231 最大子序列

    http://acm.hdu.edu.cn/showproblem.php?pid=1231 Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连 ...

  9. HDU——最大连续子序列(区间DP)

    上一个题的加强版! 最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. HDU 5092 Seam Carving (dp)

    题意,给一个数字矩阵,要求从上往下的一条路径,使这条路径上数字之和最小,如有多条输出最靠右的一条. 数字三角形打印路径... 一般打印路径有两种选择,一是转移的时候加以记录,二是通过检查dp值回溯. ...

  2. 关于java字符串常量池

    今天发现一个好玩的东西 public static void main(String[] args)    {        String str1 = new StringBuilder(" ...

  3. Java中的List接口特有的方法

    import java.util.ArrayList; import java.util.List; /* List接口中特有方法: 添加 add(int index, E element) addA ...

  4. 用Windows Native API枚举所有句柄及查找文件句柄对应文件名的方法

    枚举所有句柄的方法 由于windows并没有给出枚举所有句柄所用到的API,和进程所拥有的句柄相关的只有GetProcessHandleCount这个函数,然而这个函数只能获取到和进程相关的句柄数,不 ...

  5. SQL数据库中各种字段类型的说明

    (1)char.varchar.text和nchar.nvarchar.ntext     char和varchar的长度都在1到8000之间,它们的区别在于char是定长字符数据,而varchar是 ...

  6. debian常用指令

    查看软件xxx安装内容 dpkg -L xxx 查找软件 apt-cache search 正则表达式 查找文件属于哪个包 dpkg -S filename apt-file search filen ...

  7. 【状态压缩dp】1195: [HNOI2006]最短母串

    一个清晰的思路就是状压dp:不过也有AC自动机+BFS的做法 Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T ...

  8. 帮助解决NoSuchMethodError

    排查出具体的类,然后将冲突的类删除掉即可 Method[] methods = Base64.class.getMethods(); // 输出实际jar包路径 System.out.println( ...

  9. navicat12.0.24破解方法,简单易操作,亲测可行

    navicat12.0.24 32bit 链接:https://pan.baidu.com/s/1dakPje0AzwE86p6ZRHfnsQ 密码:f1ve 破解文件 链接:https://pan. ...

  10. Java并发编程的艺术 记录(四)

    Java线程的状态: new :初始状态,但是还没调用start方法. runnable:运行状态. blocked:阻塞状态. waiting:等待状态,表示当前线程需要等待其他线程作出一些特定动作 ...