Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 141547    Accepted Submission(s): 32929

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
 
Author
Ignatius.L
 
Recommend
We have carefully selected several similar problems for you:  1176 1069 2084 1058 1159


此题与HDU 1231一样,只是简单一些,记录前标要easy。

#include <iostream>
using namespace std;
#define M 100100
int vis[M],dp[M];
int main(int
i,int j,int k)
{
int
n,last,t,first,temp;
cin>>t;
for(
k=1;k<=t;k++)
{

cin>>n;
memset(dp,0,sizeof(dp));
for(
i=1;i<=n;i++) {cin>>vis[i];dp[i]=vis[i];}
int
sum=0,maxSum=vis[1];first=last=temp=1;
for(
i = 1; i <= n; i++)
{

sum += vis[i]; //i是从1開始的,所以先加再推断。 if(sum > maxSum){
maxSum = sum;
first = temp;
last = i;
}
if(
sum < 0){ //这里用sum表示vis[first]->vis[i]的和,所以要清零。 sum = 0;
temp = i+1;
}
}

printf("Case %d:\n",k);
printf("%d %d %d\n",maxSum,first,last);
if(
k < t)
printf("\n"); 好吧,这里就让我格式错误了几次。。。明明Case 1:和Case 2:数据中间有空行,可是完毕一次没空行。 。 。
}
return
0;
}

HDU 1003 Max Sum (动规)的更多相关文章

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

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

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

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

  3. hdu 1003 Max Sum (DP)

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

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

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

  5. HDU 1003 Max Sum【动态规划求最大子序列和详解 】

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

  6. hdu 1003 Max sum(简单DP)

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

  7. HDU 1003 Max Sum

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

  8. HDU 1003 Max Sum 解题报告

    题目大意:求一串数字中,几个连续数字加起来最大值,并确定起始和最末的位置. 思路:这是一题DP题,但是可以用尺取法来做.我一开始不会,也是看了某大神的代码,然后有人告诉我这是尺取法,现在会了. //尺 ...

  9. HDU 1003 Max Sum(AC代码)

    #include <stdio.h> int main(){ int i,t,j,n,x; int start,end,temp,max,sum; scanf("%d" ...

随机推荐

  1. CodeForces 149D 区间DP Coloring Brackets

    染色有三个条件: 对于每个点来说要么不染色,要么染红色,要么染蓝色 对于每对配对的括号来说,有且只有一个一边的括号被染色 相邻的括号不能染成相同的颜色 首先可以根据给出的括号序列计算出括号的配对情况, ...

  2. ServletResponse使用方法

    Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象 request和response对象即然代表请求和响应,那我们要 ...

  3. tabel使用总结

    对日常使用到的tabel做下记录: <table cellspacing="0"><!--单元格间距为零 默认为2 border默认为 0--> <t ...

  4. Java web 服务启动时Xss溢出异常处理笔记

    本文来自网易云社区 作者:王飞 错误日志 错误日志要仔细看,第一行不一定就是关键点,这个错误出现的时候,比较靠后,其中关键行就是下面这句. Caused by: java.lang.IllegalSt ...

  5. Wp8无广告 锁屏可以持续用的手电筒

    前面的博文写了怎么实现手电筒,界面不够漂亮 我修改了界面之后 提交到了微软的App商店 在这里送上下载地址: http://www.windowsphone.com/zh-cn/store/app/% ...

  6. IOS 自动布局-UIStackPanel和UIGridPanel(一)

    我以前是做windows phone开发的,后来转做IOS的开发,因此很多windows phone上面的开发经验也被我带到了IOS中.其实有些经验本身跟平台无关,跟平台有关的无非就是实现方法而已.好 ...

  7. [工具使用] visualvm 通过jmx不能连接

    远程服务器,通常配置下jmx,然后用visualvm连接然后监控. 但昨天自己的一台测试服务器上,正确配置了jmx还是不能连接上去. 后来参考了 https://bjddd192.github.io/ ...

  8. Leetcode 413.等差数列划分

    等差数列划分 如果一个数列至少有三个元素,并且任意两个相邻元素之差相同,则称该数列为等差数列. 例如,以下数列为等差数列: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 ...

  9. 九度oj 1011

    题目描述:     给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= ...

  10. Nginx出现403 forbidden(Permission denied)报错的四种方法

    查看nginx的error.log日志.打开日志出现Permission denied: 1.启动用户和nginx的工作用户不一致所致 查看nginx的启动用户,发现是www,而为是用root启动的 ...