HDU-1003 Max Sum(动态规划,最长字段和问题)
来源:互联网 发布:热负荷计算软件 编辑:程序博客网 时间:2024/05/26 12:54
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 193355 Accepted Submission(s): 45045
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
这是线性动态规划比较简单的最长子段和的问题,状态转移方程
if(dp[i-1]>=0)
dp[i]=dp[i-1]+a[i];
else
{
dp[i]=a[i];
}
这道题目可以用数组,也可以用滚动数组的效果,节省空间、
用一维数组
#include <iostream>#include <algorithm>#include <string.h>#include <math.h>#include <stdlib.h>using namespace std;int n;int a[100005];int dp[100005];int start;int _end;int main(){ int t; scanf("%d",&t); for(int cas=1;cas<=t;cas++) { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } memset(dp,0,sizeof(dp)); _end=1; dp[1]=a[1]; for(int i=2;i<=n;i++) { if(dp[i-1]>=0) dp[i]=dp[i-1]+a[i]; else { dp[i]=a[i]; } } int max=dp[1]; for(int i=2;i<=n;i++) { if(max<dp[i]) { max=dp[i]; _end=i; } } int t1=0; start=_end; for(int i=_end;i>0;i--) { t1=t1+a[i]; if(t1==max) start=i; } cout<<"Case "<<cas<<":"<<endl<<max<<" "<<start<<" "<<_end<<endl; if(cas!=t) printf("\n"); } return 0;}
滚动数组
#include <iostream>#include <algorithm>#include <string.h>#include <math.h>#include <stdlib.h>using namespace std;int n;int a;int sum;int _begin;int _end;int main(){ int t; scanf("%d",&t); int k=0; while(t--) { int max; int x=1; scanf("%d%d",&n,&a); sum=a; max=a; _begin=_end=1; for(int i=2;i<=n;i++) { scanf("%d",&a); if(sum>=0) { sum+=a; } else { sum=a; x=i; } if(max<sum) { max=sum; _begin=x; _end=i; } } cout<<"Case "<<++k<<":"<<endl<<max<<" "<<_begin<<" "<<_end<<endl; if(t) cout<<endl; } return 0;}
- HDU-1003 Max Sum(动态规划,最长字段和问题)
- HDU OJ 1003 Max Sum 【动态规划】
- HDU 1003 Max Sum - 简单动态规划
- HDU 1003 Max Sum 动态规划
- hdu 1003 Max Sum 动态规划
- 动态规划 1. HDU 1003 Max Sum
- hdu 1003 Max Sum 简单动态规划
- HDU 1003 Max Sum 动态规划
- 【动态规划】HDU 1003 Max Sum
- HDU 1003 Max Sum(动态规划)
- HDU-1003- Max Sum (动态规划)
- HDU 1003 Max Sum (动态规划)
- hdu-1003-Max Sum-动态规划dp
- HDU 1003 Max Sum (动态规划)
- HDU 1003 动态规划DP Max Sum
- hdu 1003 Max Sum (动态规划)
- HDU 1003 Max Sum-动态规划
- 动态规划hdu Max Sum
- 习题一-绪论
- JAVA的静态变量、静态方法、静态类
- 安卓——Toast的使用
- UESTC 第七届ACM趣味程序设计竞赛第四场(正式赛)
- 使用ProtocolBuffer或者是Wire框架进行数据传输
- HDU-1003 Max Sum(动态规划,最长字段和问题)
- 蓝懿ios 技术内容和心得 12.20
- Bios工程师手边事—SBIOS添加EC功能
- 结对编程神器:tmux
- Android开发入门:Hello,World!
- Android控件---RadioButton
- 设计模式之工厂模式
- Android Studio使用新的Gradle构建工具配置NDK环境
- keil MDK 之RTX信号量的API函数