Max Sum

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

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 <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int N = ; int a[N];
int dp[N];
int main()
{
int tcase;
scanf("%d",&tcase);
int k =;
while(tcase--){
int n;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
int start=,endd=,t=;
int mx = dp[] = a[];
for(int i=;i<=n;i++){
if(dp[i-]>=){
dp[i] = dp[i-]+a[i];
}else{
t = i;
dp[i] = a[i];
}
if(dp[i]>mx){
mx = dp[i];
start = t;
endd = i;
}
}
printf("Case %d:\n%d %d %d\n",k++,mx,start,endd);
if(tcase!=) printf("\n");
}
return ;
}

hdu 1003(最大子段和)的更多相关文章

  1. HDU 1003(A - 最大子段和)

    HDU   1003(A - 最大子段和) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/A 题目: ...

  2. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  3. dp 动态规划 hdu 1003 1087

    动态规划就是寻找最优解的过程 最重要的是找到关系式 hdu 1003 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意:求最大字序列和, ...

  4. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  5. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

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

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

  7. hdu 1003 hdu 1231 最大连续子序列【dp】

    HDU1003 HDU1231 题意自明.可能是真的进步了点,记得刚开始研究这个问题时还想了好长时间,hdu 1231还手推了很长时间,今天重新写干净利落就AC了. #include<iostr ...

  8. HDU 1003 最大连续子段和

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)M ...

  9. [ACM] hdu 1003 Max Sum(最大子段和模型)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

随机推荐

  1. 使用C#解析并运行JavaScript代码

    如果想在C#编程中解析并运行JavaScript代码,常见的方式有两种: 利用COM组件“Microsoft Script Control”,可参见:C#使用技巧之调用JS脚本方法一 利用JScrip ...

  2. 【P2602】【ZJOI2012】数字计数

    传送门 Description 给定两个正整数\(a\)和\(b\),求在\([a,b]\)中的所有整数中,每个数码(\(digit\))各出现了多少次. Input 两个正整数\(a,b\) Out ...

  3. JavaScript倒计时脚本

    JavaScript倒计时在Web中用得非常广泛,比如常见的团购啊.还有什么值得期待的事情,都可以用到倒计时.现在举了四个例子,比如时间长的倒计时,小时倒计时,最简的倒计时,还有秒表等等,应该可以满足 ...

  4. 图片上传(方法一:jquery.upload.js)

    一.在JSP页面引入jquery.upload.js 文件: <script type="text/javascript" src="${ctx}/script/j ...

  5. jenkins实现maven项目自动化部署tomcat

    最近公司有用到jenkins实现自动化部署,这里我对新的东西也是比较感兴趣,就用了点时间尝试了一下,虽然网上有很多这种例子,但是可能有些细节我也走了一些弯路.在这里记录一下,方便下次用到. 实现环境: ...

  6. python升级引发的问题总结

    http://www.cnblogs.com/ajianbeyourself/p/4214398.html http://www.cnblogs.com/hanggegege/p/6993003.ht ...

  7. oepncv-学习笔记一

    安装opencv文件时若需要cmake编译,如果中间出现 解决办法是: 在opencv的文件中找到包含cmakelist.txt的文件夹,把where is the source code:的路径改成 ...

  8. [freemarker篇]06.超级强大的自定义指令

    Freemarker的自定义指令是很强大的,非常强大,在之后的教程中我会简单的做一个示例,让大家对其有所了解!如果做Freemarker编程,请好好看看API手册,可以说里面的内容很多!也是一门独立的 ...

  9. (转)Django发送html邮件

    本文转自http://blog.csdn.net/yima1006/article/details/8991145 send_mail(subject, message, from_email, re ...

  10. WebDriver中如何处理Iframe 及 嵌套Iframe

    最近在用webdriver进行爬虫的时候,遇到了网站存在iframe的情况,处理了好久没有解决,后来发现原来webdriver自带处理方法,汗颜.. 1.iFrame有ID 或者 name的情况 // ...