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. Asp.Net MVC 高级特性(附带源码剖析)

    1. 程序入口(MvcHandler,RouteHandler,HttpModule) 2.异步类包(静态类AsyncResultWrapper),开启整个MVC异步循环 3.Aggregate递归链 ...

  2. RocketMQ中Broker的启动源码分析(二)

    接着上一篇博客  [RocketMQ中Broker的启动源码分析(一)] 在完成准备工作后,调用start方法: public static BrokerController start(Broker ...

  3. v-text,v-html等区别

    首先我们知道vue中有很多自定义指令,以v- 开头,例如:v-text,v-bind,v-model, v-if,等 在这些指令中,部分指令之间是很容易被混淆,所以今天决定自己总结一下以下几个相似指令 ...

  4. 【Java例题】4.1 级数求和1

    1. 计算级数之和: y=1-1/2+1/4-1/8+...+ (-1)^(n-1)/2^(n-1). 这里的"^"表示乘方. package chapter4; import j ...

  5. Mybatis获取代理对象

    mybatis-config.xml里标签可以放置多个environment,这里可以切换test和develop数据源 databaseIdProvider提供多种数据库,在xml映射文件里选择da ...

  6. ASP.NET Core MVC 之视图组件(View Component)

    1.视图组件介绍 视图组件是 ASP.NET Core MVC 的新特性,类似于局部视图,但它更强大.视图组件不使用模型绑定,并且仅依赖于调用它时所提供的数据. 视图组件特点: 呈块状,而不是整个响应 ...

  7. 使用Jasypt对SpringBoot配置文件加密

    # **前言** 在日前安全形势越来越严重的情况下,让我意识到在项目中存在一个我们经常忽略的漏洞,那就是我们的项目的配置文件中配置信息的安全,尤其是数据库连接的用户名和密码的安全.所以这里我们就需要对 ...

  8. apicloud 开发环境搭建

     之前做过appcan 手机应用的开发,工作需要切换的apicloud , 开发环境的的搭建是开发的第一步,let's go 1新建应用 step1  注册账号 注册apicloud 账号:https ...

  9. Mybatis案例超详解(上)

    Mybatis案例超详解(上) 前言: 本来是想像之前一样继续跟新Mybatis,但由于种种原因,迟迟没有更新,快开学了,学了一个暑假,博客也更新了不少,我觉得我得缓缓,先整合一些案例练练,等我再成熟 ...

  10. 部分APP无法代理抓包的原因及解决方法

    引言 HTTP应用层的抓包已经成为日常工作测试与调试中的重要一环,最近接触新项目突然之间发现之前的抓包手段都不好使了,顿时模块与模块之间的前端与服务之间的交互都变成了不可见,整个人都好像被蒙住了眼睛. ...