HDU-1003-Max Sum【DP】
来源:互联网 发布:淘宝模特快速摆拍 编辑:程序博客网 时间:2024/05/16 07:36
题目链接:点击打开链接
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 216929 Accepted Submission(s): 51139
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
25 6 -1 5 4 -77 0 6 -1 1 -6 7 -5
Sample Output
Case 1:14 1 4Case 2:7 1 6
大意:题解:竟然不会记录元素首尾的位置,唉,看了别人的思路
给出一个序列,求出序列中连续的数的和的最大值并输出连续数列中首尾的位置,比如6 -1 5 4 -7 中6-1+5+4=14为连续数列中和最大的。
输入:
先输入t,表示t组测试数据;
每组测试数据第一个数为序列的个数;
输出:
输出要求的最大和,和求出的序列的首尾位置。
用数组 dp [ i ] 记录以 a [ i ] 结尾的最大连续子序列,然后只需找出 dp [ i ] 的最大值就行了
对于序列中的元素 a [ i ] 只有两种情况:
1、a [ i ] 成为一个子序列的终点,就是跟 以a [ i-1]结尾的子序列接上;这时: dp [ i ] = dp [ i-1] + a [ i ]
2、a [ i ] 成为一个新子序列的起点;这时: dp [ i ] = a[ i ];
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int MAX=1e5+10;int n;int a[MAX];int dp[MAX]; // 表示以 a[i] 结尾的最大子序列 bool flag=0;int main(){int t,text=0;scanf("%d",&t);while(t--){memset(a,0,sizeof(a));memset(dp,0,sizeof(dp));int start,end;int ans=-9999999;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&a[i]); dp[i]=max(a[i],dp[i-1]+a[i]);if(ans<dp[i]){ans=dp[i];end=i;}}int sum=0;for(int i=end;i>=1;i--){sum+=a[i];if(sum==ans){start=i;}}if(flag)printf("\n");printf("Case %d:\n%d %d %d\n",++text,ans,start,end);flag=1;//if(t)printf("\n"); 第一次见这样的换行格式 就是单纯的记录一下这样的换行格式 }return 0;}
0 0
- HDU--1003 -- Max Sum [dp]
- HDU 1003 Max Sum(dp)
- HDU 1003 Max Sum(dp)
- hdu 1003 Max Sum dp
- hdu 1003 Max Sum (DP)
- HDU 1003 Max Sum(DP)
- HDU-1003-Max Sum【DP】
- HDU 1003 Max Sum DP .
- HDU-1003-Max Sum(DP)
- HDU-1003 Max Sum (dp)
- HDU- Max Sum (dp)
- hdu 1003 Max Sum (DP)
- HDU ACM 1003 Max Sum(简单DP)
- hdu-1003(java)-Max Sum (DP)
- hdu 1003 Max Sum(简单DP)
- hdu 1003 MAX sum (简单DP)
- HDU 1003:Max Sum(DP)
- HDU 1003 Max Sum(DP)
- canvas裁剪clip()函数
- VS2015 vc 项目出现new.h找不到的错误
- TextView文本过长时用省略号表示的处理方法
- 工作经验总结——说话语言
- 线性代数与矩阵
- HDU-1003-Max Sum【DP】
- Android ListView工作原理完全解析,带你从源码的角度彻底理解
- leetcode/344. Reverse String
- Axel多线程下载工具使用详解
- Android View的Measure过程(一)
- 修改MySql中数据表和字段的字符集和排序规则
- 345. Reverse Vowels of a String*
- 预处理命令: #if ,#ifdef ,#ifndef
- centsos7 maridb修改过密码之后执行mysql进不去