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. Python数据类型(简单入门)

    数据类型(预了解) 1.数字类型 整型:int 即不带小数点的数,通常用来标识年龄,账号,身份证号,等级等整数. 浮点型:float 即带有小数点的数,通常用来标记身高,体重,科学计算等有小数点的数. ...

  2. Python Cookbook3 Python进阶教程 http://python3-cookbook.readthedocs.io/zh_CN/latest/copyright.html

    http://python3-cookbook.readthedocs.io/zh_CN/latest/copyright.html

  3. iOS UITextView点击事件处理

    自定义一个UITextView UITextView 的selectedRange 影响 selectedTextRange 改变前者可影响后者 self.selectedRange -->se ...

  4. (转)iOS开发之同一应用设置不同图标和名称

    本文转自:http://www.devzeng.com/blog/ios-two-version-app-setting-profile.html iOS开发之同一应用设置不同图标和名称 SEP 6T ...

  5. re--模块【转】

    为什么要学正则表达式 实际上爬虫一共就四个主要步骤: 明确目标 (要知道你准备在哪个范围或者网站去搜索) 爬 (将所有的网站的内容全部爬下来) 取 (去掉对我们没用处的数据) 处理数据(按照我们想要的 ...

  6. C++ 实验六

    Part.2 // 合并两个文件内容到一个新文件中. // 文件名均从键盘输入 #include <iostream> #include <fstream> #include ...

  7. 关于学习Mongodb的几篇文章

    一.Mongodb分片的使用 http://www.caiyiting.com/blog/2014/mongodb-sharding.html 二.MongoDB分布式高可用集群实现 http://w ...

  8. luogu4301 [CQOI2013]新Nim游戏

    nim和线性基 #include <algorithm> #include <iostream> #include <cstdio> using namespace ...

  9. 九度oj 题目1153:括号匹配问题

    题目描述: 在某个字符串(长度不超过100)中有左括号.右括号和大小写字母:规定(与常见的算数式子一样)任何一个左括号都从内到外与在它右边且距离最近的右括号匹配.写一个程序,找到无法匹配的左括号和右括 ...

  10. PAT天梯赛练习题——L3-007. 天梯地图(多边权SPFA)

    L3-007. 天梯地图 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题要求你实现一个天梯赛专属在线地图,队员输入自己学校 ...