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. 机器学习12—FP-growth学习笔记

    test12.py #-*- coding:utf-8 import sys sys.path.append("fpGrowth.py") import fpGrowth from ...

  2. PHP浮点数运算精度造成的,订单金额支付经常少1分的问题

    最近碰见一个奇怪的问题,商城通过微信支付的订单经常少一分钱,经过排查是PHP浮点运算精度问题造成的 由PHP浮点数运算精度造成的,鸟哥的Bolg有详细的说明.http://www.laruence.c ...

  3. 常用PhpStorm 快捷键

    函数列表 打开某一个源码文件后,保证鼠标焦点在源文件内,按键盘组合键: alt + 7 返回原文件导航:双击最上面的工程名即可 PhpStorm折叠文件内所有函数 按下快捷`Ctrl`+`Shift` ...

  4. publish over ssh 实现 Jenkins 远程部署

    Jenkins远程部署,一开始没有任何头绪,想了很多方案. 因为两台机器都是windows系统,所以想到publish over cifs, 但是这个网上资料太少,貌似只能内网使用.又想到了Jenki ...

  5. Python修饰器的函数式编程(转)

    From:http://coolshell.cn/articles/11265.html 作者:陈皓 Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Desi ...

  6. Java static关键字特点

    一.特点 1.随着类的加载而加载2.优先于对象存在3.被类的所有对象共享4.可以通过类名调用 二.调用特征 静态方法: 成员变量:只能访问静态变量 成员方法:只能访问静态成员方法 非静态方法: 成员变 ...

  7. Muduo网络库源代码分析(四)EventLoopThread和EventLoopThreadPool的封装

    muduo的并发模型为one loop per thread+ threadpool.为了方便使用,muduo封装了EventLoop和Thread为EventLoopThread,为了方便使用线程池 ...

  8. Educational Codeforces Round 1 (C) (atan2 + long double | 大数)

    这题只能呵呵了. 东搞西搞,折腾快一天,最后用了一个800多行的代码AC了. 好好的题目你卡这种精度干啥. 还有要卡您就多卡点行不,为什么long double 又可以过... 废了N长时间写个了不管 ...

  9. linux下jdk多版本管理

    linux下jdk多版本管理 项目开发中,不管是哪种语言都避免不了多个版本环境管理问题(本文虽然以jdk为例来写的,但不仅限于jdk),如何能做到快速的环境升级与切换确实是一件深思的事! 安装jdk ...

  10. Js中的Object.defineProperty

    通过Object.defineProperty为对象设置属性,并同时规定属性的属性(可见性,可配置性,可枚举性等) 备注:如果通过var obj = {} obj.age = 18这种方式设置的属性, ...