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. Educational Codeforces Round 27 F. Guards In The Storehouse

    F. Guards In The Storehouse time limit per test 1.5 seconds memory limit per test 512 megabytes inpu ...

  2. redhat安装中文man手册

    1.下载中文man手册 http://download.chinaunix.net/download.php?id=13232&ResourceID=6537 2.上传至服务器并解压 tar ...

  3. 【Python学习】之yagmail库实现发送邮件

    上代码: import yagmail sendmail = 'xxx@126.com' sendpswd = 'xxx' receivemail = 'xxx@qq.com' # 连接邮箱服务器 y ...

  4. GT背靠背onsite

    Google: 因为暑假在G家实习过,所以仅仅是简单面了2轮. 后来跟曾经的intern host吃饭得知,他和还有一个reviewer对我的实习工作都给了不错的评价,所以面试的时候面试官都放水了.题 ...

  5. 很全的php数组操作方法

    一.数组操作的基本函数 数组的键名和值 array_values($arr);获得数组的值 array_keys($arr);获得数组的键名 array_flip($arr);数组中的值与键名互换(如 ...

  6. POJ3468_A Simple Problem with Integers(线段树/成段更新)

    解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...

  7. 一个手动备份MySQL数据库的脚本

    #!/bin/bash username=root hostname=localhost password=root mysql -u$username -h$hostname -p$password ...

  8. Obj-C数组以及字符串拼接与分割

    本文转载至 http://mobile.51cto.com/iphone-392148.htm Obj-C只是增加了一点“特殊语料”的C语言,所以可以用printf()代替NSLog().但我们建议使 ...

  9. Jenkins maven仓库地址 和 手动修改maven 版本

    ① Jenkins maven仓库地址,一般情况会在:/root/.m2/repository/* ② 手动修改maven 版本,Apache 下载指定的maven版本,然后解压后copy到指定目录即 ...

  10. NSTheard 详解

    一.什么是NSThread NSThread是基于线程使用,轻量级的多线程编程方法(相对GCD和NSOperation),一个NSThread对象代表一个线程, 需要手动管理线程的生命周期,处理线程同 ...