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. hdu 6397 Character Encoding (生成函数)

    Problem Description In computer science, a character is a letter, a digit, a punctuation mark or som ...

  2. 放出一批学生管理系统jsp源码,部分有框架

    基于jsp+struts 2的学生管理系统eclipse - 源码码头   https://www.icodedock.com/article/25.html 基于jsp+mysql的JSP学生成绩管 ...

  3. 让 CXK 来教你实现游戏中的帧动画(上)

    一款游戏除了基本功能之外,还需要给玩家更多视觉上的刺激,这个时候就需要用特效来装饰.本文就将介绍 Cocos Creator 的动画系统,除了标准的位移.旋转.缩放动画和序列帧动画以外,这套动画系统还 ...

  4. 分布式ID系列(4)——Redis集群实现的分布式ID适合做分布式ID吗

    首先是项目地址: https://github.com/maqiankun/distributed-id-redis-generator 关于Redis集群生成分布式ID,这里要先了解redis使用l ...

  5. Ant Design Pro 脚手架+umiJS 实践总结

    一.简介 1.Ant Design Pro Ant Design Pro是一款搭建中后台管理控制台的脚手架 ,基于React,dva.js,Ant Design (1)其中dva主要是控制数据流向,是 ...

  6. maven的不同版本下载及环境配置

    Maven不同版本下载及环境配置 Maven下载 去到官网 https://maven.apache.org/ 会发现是最新版本,但是一般下载的话,都会下载比最新的版本要低两到三个小版本的,这里就下载 ...

  7. Jenkins持续集成项目搭建——基于Python Selenium自动化测试

    参考链接:https://www.liaoxuefeng.com/article/1083282007018592 第一步:去官网https://jenkins.io/下载最新的war包 第二步:安装 ...

  8. 洛谷 P3628 [APIO2010]特别行动队

    题意简述 将n个士兵分为若干组,每组连续,编号为i的士兵战斗力为xi 若i~j士兵为一组,该组初始战斗力为\( s = \sum\limits_{k = i}^{j}xk \),实际战斗力\(a * ...

  9. C语言编程入门之--第五章C语言基本运算和表达式-part3

    5.3  挑几个运算符来讲 常用的运算符除了加减乘除(+-*/)外,还有如下: 注意:以下运算符之间用逗号隔开,C语言中也有逗号运算符,这里不讲逗号运算符. 1. 赋值运算符 =,+=,*= 2. 一 ...

  10. Code signing is required for product type 'Unit Test Bundle' in SDK 'iOS 11.0.1'

    Code signing is required for product type 'Unit Test Bundle' in SDK 'iOS 11.0.1' 进入 projects and lis ...