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. Python—推导式

    推导式 推导式:comprehensions(又称解析式),是Python的一种独有特性,相当于语法糖的存在,推导式是可以从一个数据序列构建另一个新的数据序列的结构体. 共有三种推导,在Python2 ...

  2. IO流的Properties集合,序列化流与反序列化流,打印流及commons-IO

    内容介绍 Properties集合 序列化流与反序列化流 打印流 commons-IO Properties类 Properties类介绍 Properties 类表示了一个持久的属性集.Proper ...

  3. 对于HTTP过程中POST内容加密的解决方案

    0x00前言 前几天我师傅和我提及了这件事情 正常情况下 抓包过程中遇到加密情况会很迷茫 昨天把这个都弄了一下 也感谢大佬中间的指导 我一开始看到密码的类型下意识的是base64 但是去解密发现不对 ...

  4. 用mongodb 固定集合实现只保留固定数量的记录,自动淘汰老旧数据

    在一个保存report记录的场景中,我们使用MongoDB进行数据存储 example: db: report Collection: daily_report 创建db:  use report; ...

  5. CentOS 7.3下使用YUM 安装MySQL5.6

    1.检查Linux系统中是否已安装 MySQL rpm -qa | grep mysql 返回空值的话,就说明没有安装 MySQL 注意:在新版本的CentOS7中,默认的数据库已更新为了Mariad ...

  6. what is the CCA?

    Clear Channel Assessment (CCA) is one of two carrier sense mechanisms in WLAN (or WiFi). It is defin ...

  7. CentOS yum 源修改

    修改 CentOS 默认 yum 源为 mirrors.163.com 首先备份系统自带yum源配置文件/etc/yum.repos.d/CentOS-Base.repo [root@localhos ...

  8. ASP.NET CORE 2.* 利用集成测试框架覆盖HttpClient相关代码

    ASP.NET CORE 集成测试官方介绍 我的asp.net core 项目里面大部分功能都是去调用别人的API ,大量使用HttpClient,公司单元测试覆盖率要求95%以上,很难做到不mock ...

  9. flask 使用基础

    转 https://blog.csdn.net/yelena_11/article/details/53404892

  10. 【win10主机】连接virtualbox上【32位winXP系统虚拟机】上启动的mysql

    问题Q: 在virtualbox上启动winXP系统虚拟机后,启动含oa项目的tomcat,数据库服务也运行起来了,虚拟机上连接无误: 在上一篇<主机访问 虚拟机启动的项目>基础上,尝试连 ...