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. Java -- 数字

    @.运用BigDecimal处理Double类型的算术运算的精度问题 原文:https://blog.csdn.net/j754379117/article/details/52238396 可使用 ...

  2. Android 浏览器文本垂直居中问题

    问题描述 在开发中,我们常使用 line-height 属性来实现文本的垂直居中,但是在安卓浏览器渲染中有一个常见的问题,就是对于小于12px的字体使用 line-height 属性进行垂直居中的时候 ...

  3. ios -富文本和尺寸

    /** *  计算文本的宽高 方法 2 * *  @param str     需要计算的文本 *  @param font    文本显示的字体 *  @param maxSize 文本显示的范围 ...

  4. ios8 一些运行问题

     iOS10相册相机闪退bughttp://www.jianshu.com/p/5085430b029fiOS 10 因苹果健康导致闪退 crashhttp://www.jianshu.com/p/5 ...

  5. android webview 加载本地html 实现 与 java 之间的相互响应

    1.布局 <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:androi ...

  6. Linux中的关机

    我是用普通用户登录,在终端下输入shutdown命令,结果显示 command not found.这就奇怪了,难道我的linux不支持这个命令?man了一下shutdown,大篇幅的说明告诉我,我的 ...

  7. ASIHTTP

    本文转载至 http://www.th7.cn/Program/IOS/201303/128223.shtml     向服务器端上传数据 ASIFormDataRequest ,模拟 Form表单提 ...

  8. windowsphone8.1学习笔记之位图编程

    说位图,先把image控件简单过下,Image的Source设置 <Image Name="img" Source="可以是网络图片的Uri.应用文件的Uri或者安 ...

  9. [Android]豆瓣FM离线数据

    离线目录结构: /sdcard/Android/data/com.douban.radio下 ./cache/fileCaches: 离线音乐歌词(lyric) ./cache/images: 离线音 ...

  10. can t connect to mysql server on 'localhost'解决方法

    源:http://www.111cn.net/database/mysql/37700.htm 先重启服务器可解决大部分问题 错误编号:2003 问题分析: 无法连接到 mysql 服务器,可能的情况 ...