Max Sum

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

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
 
算法分析:求最大字段和,d[i]表示已 i 结尾(字段和中包含 i )在 a[1..i] 上的最大和,d[i]=(d[i-1]+a[i]>a[i])?d[i-1]+a[i]:a[i];
max = {d[i],1<=i<=n} ;
 #include<iostream>
#define N 100010
using namespace std;
int a[N],d[N];
int main()
{
int test,n,i,max,k,f,e;
cin>>test;
k=;
while(test--)
{
cin>>n;
for(i=;i<=n;i++)
cin>>a[i];
d[]=a[];
for(i=;i<=n;i++)
{
if(d[i-]<) d[i]=a[i];
else d[i]=d[i-]+a[i];
}
max=d[];e=;
for(i=;i<=n;i++)
{
if(max<d[i])
{
max=d[i];e=i;
}
}
int t=;
f=e;
for(i=e;i>;i--)
{
t=t+a[i];
if(t==max) f=i;
}
cout<<"Case "<<k++<<":"<<endl<<max<<" "<<f<<" "<<e<<endl;
if(test) cout<<endl;
}
return ;
}

改进后的只处理最大和不处理位置

 #include<cstdio>
int main()
{
int n,test,ans,t,a,i;
scanf("%d",&test);
while(test--)
{
scanf("%d",&n);
scanf("%d",&a);
ans=t=a;
for(i=;i<n;i++)
{
scanf("%d",&a);
if(t<) t=a;
else t=t+a;
if(ans<t) ans=t;
}
printf("%d\n",ans);
}
return ;
}
 #include <bits/stdc++.h>
using namespace std; int main()
{
int T;
scanf("%d", &T); int N;
int a;
int ans;
int sum;
int i;
int bg, ed;//起始,结束
int bg2;
int cas = ; while (T--) {
scanf("%d", &N); ans = -;//
sum = ;//
bg2 = ;//默认起始位置
for (i = ; i < N; ++i) {
scanf("%d", &a); sum = sum + a;
if (sum > ans) {
ans = sum;
bg = bg2;
ed = i;
}
if (sum < ) {//< 0 那么这段到此为止吧
sum = ;//
bg2 = i + ;//更新起始位置
}
} printf("Case %d:\n", ++cas);
printf("%d %d %d\n", ans, bg + , ed + );
if (T > ) {
printf("\n");
}
}
return ;
}

这个和上面的相比写法容易理解些

 #include <bits/stdc++.h>
using namespace std; int main()
{
int T;
scanf("%d", &T); int N;
int a;
int ans;
int sum;
int i;
int bg, ed;//起始,结束
int bg2;
int cas = ; while (T--) {
scanf("%d", &N); scanf("%d", &a);
ans = a;
sum = a;
bg = , ed = ;
bg2 = ; for (i = ; i < N; ++i) {
scanf("%d", &a); if (sum <= ) {//从样例2 看,这里要<,但是<= 也可以,只要找到一个最大的子串就可以
sum = a;
bg2 = i;
} else {
sum = sum + a;
} if (sum >= ans) {//这里> 和>= 都可以
ans = sum;
bg = bg2, ed = i;
}
} printf("Case %d:\n", ++cas);
printf("%d %d %d\n", ans, bg + , ed + );
if (T > ) {
printf("\n");
}
}
return ;
}

hdu 1003 Max Sum(基础dp)的更多相关文章

  1. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  2. hdu 1003 Max sum(简单DP)

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

  3. HDU 1003 Max Sum(DP)

    点我看题目 题意 : 就是让你从一个数列中找连续的数字要求他们的和最大. 思路 : 往前加然后再判断一下就行. #include <iostream> #include<stdio. ...

  4. HDU - 1003 Max Sum 【DP】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1003 题意 给出一个序列 要求找出一个和最大的子序列 思路 O(N)的做法 但是要标记 子序列的头部位 ...

  5. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  6. hdu 1003 Max Sum (DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)   ...

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

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

  8. 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 ...

  9. HDU 1003 Max Sum【动态规划求最大子序列和详解 】

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

随机推荐

  1. pip安装错误,用镜像

    Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'Connec ...

  2. ImportError: No module named '_sqlite3'

    问题: Python 3.5.1 报错如下 Traceback (most recent call last): File "manage.py", line 16, in < ...

  3. 更改Mysql 密码的4种方法(转)

    原文:http://www.jb51.net/article/39454.htm 方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password f ...

  4. [root@localhost ~]#各项解释

    [root@localhost ~]# 解释: [登录用户@主机名 索引目录(~家目录,当前所在的目录)]#号代表超级用户,$普通用户

  5. 仿照ArrayList自己生成的MyList对象

    现在需要自己生成一个list集合,基本雷同ArrayList,不使用API的List接口. 实现如下: MyList的代码: public class MyList<T> { privat ...

  6. 【BZOJ3769】spoj 8549 BST again DP(记忆化搜索?)

    [BZOJ3769]spoj 8549 BST again Description 求有多少棵大小为n的深度为h的二叉树.(树根深度为0:左右子树有别:答案对1000000007取模) Input 第 ...

  7. 解决Eclipse的Team菜单中没有SVN选项的问题

    我们想使用SVN向SVN服务器上传代码,但Eclipse默认情况下却没有SVN选项,如下图所示. 默认只有GIT,如下图所示. 那么,我们怎么解决这个问题呢? 第一步:如下图所示. 第二步:在&quo ...

  8. api 爬虫 避免相同 input 在信息未更新 情况下 重复请求重复

  9. spring mvc 基本原理

    在web.xml配置spring mvc入口servlet: <servlet> <servlet-name>mvc-dispatcher</servlet-name&g ...

  10. 我的Android进阶之旅------>如何将Android源码导入Eclipse中来查看(非常实用)

    Android源码下载完成的目录结构如如所示: step1:将.classpath文件拷贝到源代码的根目录 Android源码支持多种IDE,如果是针对APP层做开发的话,建议大家使用Eclipse开 ...