Max Sum

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
#include <bits/stdc++.h>
using namespace std;
const int MAXN = + ;
int T, n;
int arr[MAXN], dp[MAXN];
int S, E;
int main() {
scanf("%d", &T);
for(int t = ; t < T; ++t) {
S = E = ;
cin >> n;
for(int i = ; i != n; ++i)
cin >> arr[i];
dp[] = arr[];
for(int i = ; i != n; ++i)
if(dp[i-] >= )
dp[i] = dp[i-] + arr[i];
else
dp[i] = arr[i];
int Max = dp[];
for(int i = ; i != n; ++i)
if(dp[i] >= Max) {
Max = dp[i];
E = i;
}
int sum = ;
for(int i = E; i >= ; --i) {
sum += arr[i];
if(sum == Max)
S = i;
}
cout << "Case " << t+ << ":" << endl;
cout << Max << " " << S+ << " " << E+ << endl;
if(t < T-) puts("");
}
return ;
}

用了数组

#include <bits/stdc++.h>
using namespace std; int main() {
int T,n;
int Max, S, E, sum, a;
cin >> T;
for(int t = ; t <= T; ++t) {
cin >> n;
S = E = sum = ;
Max = -;
int k = ;
for(int i = ; i != n; ++i) {
cin >> a;
sum += a;
if(sum > Max) {
Max = sum;
S = k;
E = i;
}
if(sum < ) {
sum = ;
k = i + ;
}
}
cout << "Case " << t << ":" << endl;
cout << Max << " " << S+ << " " << E+ << endl;
if(t < T) puts("");
}
return ;
}

不用数组

HDU1003 简单DP的更多相关文章

  1. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  3. codeforces Gym 100500H A. Potion of Immortality 简单DP

    Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...

  4. 简单dp --- HDU1248寒冰王座

    题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...

  5. poj2385 简单DP

    J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  6. hdu1087 简单DP

    I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     ...

  7. poj 1157 LITTLE SHOP_简单dp

    题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...

  8. hdu 2471 简单DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2571 简单dp, dp[n][m] +=(  dp[n-1][m],dp[n][m-1],d[i][k ...

  9. Codeforces 41D Pawn 简单dp

    题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...

随机推荐

  1. Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

    最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...

  2. 整合s2sh,实现页面操作数据库

    先说点废话 s2sh,就是struts2,spring,hibernate:s2作为表现层和控制器,hibernate作为持久层,spring作为业务层(充分应用IOC和AOP).其实业务还是业务,只 ...

  3. 在Windows Server 2008中布置Web站点时遇到的问题及解决办法

    首先安装了VS2012. 首先在计算机--管理 中添加服务器角色, 添加角色: 进行各种设置: 选择对应的应用程序池,原来默认的是: 需要添加一个4.0的. 添加后,原因:在安装Framework v ...

  4. bootstrap做了一个表格

    花了一下午做了一个表格: 大致是这样: 代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf ...

  5. 关于Visual Studio 2015中没有报表项(ReportViewer)的解决方案。

    没有报表,一般默认安装之后会出现这种情况,在安装的时候选择自定义安装,把Microsoft Office 开发人员工具.Microsoft SQL Server Data Tools勾选上,安装之后就 ...

  6. Test Regular Expressions Online with RegExr免费的正则表达式检验网站

    免费的正则表达式检验网站: http://www.regexr.com

  7. JavaScript对象的理解 及 字面量对象与数组的关系

    JavaScript的简单类型包括 数字.字符串.布尔值.null值.undefined值. 其他的值都是对象.对象是可变的键控集合.数组.函数.正则表达式都是对象. 对象是属性的容器,属性都是名字和 ...

  8. css-单位%号-background-size-background-position-遁地龙卷风

    (-1)写在前面 我用的是chrome49,这篇是为后续做准备.重要性的调整以及毕业资料的整体导致最近没看JQuery和H5特效,以后只能晚上看了. (0)准备 div长宽都为300px,我们一张大小 ...

  9. EasyUI创建异步树形菜单和动态添加标签页tab

    创建异步树形菜单 创建树形菜单的ul标签 <ul class="easyui-tree" id="treeMenu"> </ul> 写j ...

  10. 在MVC中实现文件的上传

    @using (Html.BeginForm("daoru", "Excel", FormMethod.Post, new { enctype = " ...