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. GPnP profile内容

    <?xml version="1.0" encoding="UTF-8"?>  <gpnp:GPnP-Profile Version=&quo ...

  2. codeforces Gym 100286J Javanese Cryptoanalysis (二染色)

    每一单词相邻两个字母,不能同时为元音或者辅音... 各种姿势都可以过:7个for,dp,黑白染色,dfs,并查集.... 最主要的思路就是相邻字母连边,把元音和辅音看成两个集合,那么有连边的两个字母一 ...

  3. UVA1610 PartyGame 聚会游戏(细节题)

    给出一组字符串D,要找一个字符串S使得D中一半小于等于S,另外一半大于S.输入保证一定有解.长度要尽量短,在此基础上字典序尽量小. 分类谈论,细节挺多的,比如'Z'. 其实直接暴就过了,没分类辣么麻烦 ...

  4. 剑指offer22 栈的压入、弹出序列

    写的一个代码,虽然正确通过了,但我觉得会报vector越界的错误 class Solution { public: bool IsPopOrder(vector<int> pushV,ve ...

  5. 使用Python生成ASCII字符画

    使用Python生成ASCII字符画 在很多的网站主页中或者程序的注释中会有一些好看的字符注释画.显得很牛逼的样子 例如: 知乎 _____ _____ _____ _____ /\ \ /\ \ / ...

  6. Vue 后台管理

    这里是结合vue和element快速成型的一个demo 里面展示了基本的后台管理界面的大体结构和element的基本操作 GitHub的地址:https://github.com/wwwming/ad ...

  7. c++ 读取文件 最后一行读取了两次

    用ifstream的eof(),竟然读到文件最后了,判断eof还为false.网上查找资料后,终于解决这个问题. 参照文件:http://tuhao.blogbus.com/logs/21306687 ...

  8. ps基础实例

    一:合并多个图片 1.先新件一个图片)CTRL+N),大小定成你想要的大小 2.把你要放入的照片用PS打开 3.把放入的照片用移动工具(V)拉到新件的图片里面 4.用CTRL+T调整大小(按住SHIF ...

  9. 通过脚本批量添加AD用户

    1.新建一个csv文件(逗号分隔的一种值文件) 内容为:放在C:\盘根目录下 test300 test300 .com test300 test301 test301 .com test301 tes ...

  10. Bootstrap 提示工具(Tooltip)插件方法的用法

    方法 下面是一些提示工具(Tooltip)插件中有用的方法: 方法 描述 实例 Options: .tooltip(options) 向元素集合附加提示工具句柄. $().tooltip(option ...