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. 如何在github开源自己的项目

    1.到GitHub上注册自己的账号.https://github.com/ 2.创建第一个代码仓库. 选择public,public权限表示所有人都能够查看这些代码并下载.然后点击Create rep ...

  2. Python flask构建微信小程序订餐系统

    第1章 <Python Flask构建微信小程序订餐系统>课程简介 本章内容会带领大家通览整体架构,功能模块,及学习建议.让大家在一个清晰的开发思路下,进行后续的学习.同时领着大家登陆ht ...

  3. 7z 命令行方式生成自解压exe

    一.下载 7z是一个免费的工具,除了通过命令行的方式提供各种文件.压缩包相关的操作外,还提供了一种方式可以打出自解压的exe程序.该程序从运行到结束经历了三个流程: (1) 解压文件到用户临时目录: ...

  4. 数据结构之二叉树的构建C++版

    二叉树的构建要注意与链式表的区别,二叉树这里的构建十分低级,每个树只是构建了一个单一的二叉树节点,总体来看是有下向上构建的.用户需要手动去构建自己需要的树,而不是直接去插入数据就到二叉树中了,因为不是 ...

  5. 想成为顶尖 Java 程序员?请先过了下面这些技术问题。

    一.数据结构与算法基础 说一下几种常见的排序算法和分别的复杂度. 用Java写一个冒泡排序算法 描述一下链式存储结构. 如何遍历一棵二叉树? 倒排一个LinkedList. 用Java写一个递归遍历目 ...

  6. ubuntu安装伪分布式Hadoop3.1.2

    作业要求:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/3223 本文是基于已经安装好的ubuntu环境上搭建伪分布式hadoop,在 ...

  7. java并发编程(二十五)----(JUC集合)LinkedBlockingDeque和ConcurrentLinkedDeque介绍

    Queue除了前面介绍的实现外,还有一种双向的Queue实现Deque.这种队列允许在队列头和尾部进行入队出队操作,因此在功能上比Queue显然要更复杂. LinkedBlockingDeque 我们 ...

  8. 映射&集合

    哈希函数 通过哈希表可以实现 O(1) 复杂度的查找. 哈希函数构造方法:设计好的哈希函数的两个基本原则,计算简单+分布均匀 1. 直接定址法 用key自身的某个线性函数来定址,f(key) = a* ...

  9. Python入门基础(10)_异常_1

    最近有点忙,到现在快一个月没写了,罪过罪过,继续学习 异常:python程序在运行时,如果python解释器遇到一个错误,那么程序就会停止执行,并且会提示一些错误信息,这就是异常. 抛出异常:程序停止 ...

  10. Javasrcipt中从一个url或者从一个字符串中获取参数值得方法

    从url中获取参数值是che程序开发过程中的常用需求,偶然得闲,便抽空研究了一下javasrcipt下,获取参数的办法(JAVA中也类似). 首先看url的规范: URL组成:protocol :// ...