Max Sum

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

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
这道题目写的时候WA了很多次
最开始用结构体写的,写了两层循环,然后自己琢磨的瞎改,一直都是两层循环
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
int a[];
struct node
{
int num1,num2,maxn;
} b[];
bool cmp(struct node a,struct node b)
{
return a.maxn > b.maxn;
}
int main()
{
int t;
scanf("%d",&t);
for(int p=; p<=t; p++)
{
int n;
scanf("%d",&n);
for(int i=; i<=n; i++)
{
b[i].num1=;
b[i].num2=;
b[i].maxn=;
}
for(int i=; i<=n; i++)
scanf("%d",&a[i]);
int j=;
for(int k=; k<=n; k++)
{
if(a[k]>=)
{
if(j!=)
j++;
b[j].maxn += a[k];
b[j].num1 = k;
int f = k;
for(int i=k+; i<=n; i++)
{
if(a[i]<)
{
b[j].num2 = i-;
int d = j;
b[++j].maxn = b[d].maxn + a[i];
b[j].num1 = f;
}
if(a[i]>=)
{
b[j].maxn += a[i];
}
if(i==n)
{
if(a[i]>=)
{
b[j].maxn += a[i];
b[j].num2 = i;
}
}
}
}
}
/*for(int i=1;i<j;i++)
printf("%d\n",b[i].maxn);
printf("=========\n");*/
sort(b+,b+j,cmp);
/*for(int i=1;i<j;i++)
printf("%d\n",b[i].maxn);*/
printf("Case %d:\n",p);
printf("%d %d %d\n",b[].maxn,b[].num1,b[].num2);
if(p!=t)
printf("\n");
}
return ;
}

后来我师傅指导了我,直接用一层循环就解决了。。

就是用sum不停累加,如果sum小于0了,就重置sum等于当前的a[i],同时更新起始,结束位置

(注意sum最大时有可能为负数,所以定义maxn时要注意maxn取值要为最小--1001 )

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
int a[];
int main()
{
int t;
scanf("%d",&t);
for(int p=; p<=t; p++)
{
int n;
scanf("%d",&n);
for(int i=; i<=n; i++)
scanf("%d",&a[i]);
int sum=,num1=,tmp=,num2=,maxn=-;//注意maxn的取值
for( int i = ; i <= n ; i++ )
{
sum += a[i] ;
if( sum > maxn )//同时跟新最大值,起始位置,结束位置
{
maxn = sum ;
num2 = i ;
num1 = tmp ;
}
if( sum < )
{
sum = ;
tmp = i + ;
}
}
printf("Case %d:\n",p);
printf("%d %d %d\n",maxn,num1,num2);
if(p!=t)
printf("\n");//还有这个输出要注意!!!最后一组数据不要输出多余空行,其他还要多输出一行空行
}
return ;
}

这段时间学dp学到的dp解法

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std;
int a[],sum[],s[];
int main(){
int T;
cin >> T;
for(int k=;k<=T;k++){
int n;
cin >> n;
for(int i=;i<n;i++){
cin >> a[i];
}
int ans = ;
sum[] = a[];
s[] = ;
for(int i=;i<n;i++){
if(sum[i-] >= ){//只要不是小于零就可以继续加,记下每次加后得到的值
sum[i] = sum[i-] + a[i];
s[i] = s[i-];
}
else{//小于零重新开始累加
sum[i] = a[i];
s[i] = i;
}
if(sum[ans] < sum[i]){//求出记录中的最大值
ans = i;
}
}
cout << "Case " << k << ":" << endl;
cout << sum[ans] << " " << s[ans]+ << " " << ans + << endl;
if(k!=T){
cout << endl;
}
}
return ;
}

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【动态规划求最大子序列和详解 】

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

  5. HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)

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

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

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

  7. hdu 1003 Max Sum (动态规划)

    转载于acm之家http://www.acmerblog.com/hdu-1003-Max-Sum-1258.html Max Sum Time Limit: 2000/1000 MS (Java/O ...

  8. HDU 1003 Max Sum

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

  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. light oj 1159 - Batman LCS

    学过简单动态规划的人应该对最长公共子序列的问题很熟悉了,这道题只不过多加了一条字符串变成三条了,还记得,只要把状态变成三维的即可. //http://lightoj.com/volume_showpr ...

  2. Chrome浏览器F12开发者工具简单使用

    1.如何调出开发者工具 按F12调出 右键检查(或快捷键Ctrl+Shift+i)调出 2.开发者工具初步介绍 chrome开发者工具最常用的四个功能模块:元素(ELements).控制台(Conso ...

  3. Go slice:切片的“陷阱”和本质

    文章说明 总结了go语言中切片slice的特殊性和使用时的注意事项. 个人理解,不足之处欢迎指出. slice:切片,是go语言中一种常用的数据结构,基于数组构建,表示相同数据类型的集合. 数组 Go ...

  4. 四、Python基础(1)

    目录 四.Python基础(1) 四.Python基础(1) 1.什么是变量? 一种变化的量,量是记录世界上的状态,变指得是这些状态是会变化的. 2.为什么有变量? 因为计算机程序的运行就是一系列状态 ...

  5. The philosophy of ranking

    In the book Decision Quality, one will be trained to have three decision making system; one of them ...

  6. Spring系列(二):Spring IoC应用

    一.Spring IoC的核心概念 IoC(Inversion of Control  控制反转),详细的概念见Spring系列(一):Spring核心概念 二.Spring IoC的应用 1.定义B ...

  7. if IE语句 | 判断浏览器IE版本及添加升级提示

    本文引自:http://blog.csdn.net/u013372487/article/details/48521929 实现方法 判断当前浏览器是否IE6(或IE6内核) <!--[if I ...

  8. Web开发中的相对路径和绝对路径

    在学习HTML的时候一定会遇到引入文件和链接跳转页面,比如:JS文件.CSS文件.Image图片.我们就会考虑是相对路径和绝对路径的问题.下面PHP程序员雷雪松就详细讲解下Web开发中的相对路径和绝对 ...

  9. Java——检测其他线程的状态以及启动已死亡的线程

    这次这个的思路是在主类中维护一个map,map的key是线程名,value是线程的状态,然后创建周期执行的线程通过检测这个map来判断进程的状态,如果有死亡的进程就把该进程启动. 首先是主类,这里的m ...

  10. vue 辅助开发工具(利用node自动生成相关文件,自动注册路由)

    vue 辅助开发工具 前言 有没有因为新建view,component,store的繁琐操作而苦恼,需要新建文件件,新建vue文件,新建js文件,注册路由...等一系列无价值操作浪费时间,为了解决这个 ...