hdu1003(动态规划,最大子序列和)
2018-01-22 22:05
337 查看
Max Sum
Time Limit: 1000 MS Memory Limit: 32768 KB64-bit integer IO format: %I64d , %I64u Java class name: Main
[Submit] [Status]
[Discuss]
Description
Given a sequence a[1],a[2],a[3]......a, 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
//并查集.cpp #include<bits/stdc++.h> using namespace std;//由于本题从前往后更新,所以不用另开数组 int dp[100005];//dp[i]记录的是以dp[i]为结尾的最大值; int main() { int n;cin>>n; while(n--) { int cnt=1; int t;cin>>t; memset(dp,0,sizeof(dp)); for(int i=1;i<=t;i++) { scanf("%d",&dp[i]); } int l=1,r=1;//记录区间 int temp=1;//记录起点 int MAx=dp[1]; for(int i=2;i<=t;i++) { if(dp[i-1]>0)//dp[i-1]大于0,则对dp[i]有贡献 { dp[i]=dp[i-1]+dp[i]; }else{temp=i;} if(dp[i]>MAx) { MAx=dp[i]; l=temp; r=i; } } if(cnt>=2)printf("\n"); printf("Case %d:\n",cnt++); printf("%d %d %d\n",MAx,l,r); } }
相关文章推荐
- HDU1003 动态规划,最大子序列之和
- HDU1003 Max Sum(动态规划,最大子序列和)
- 递增子序列最大和(最长递增子序列) 动态规划
- 动态规划_最大非降子序列的长度
- [动态规划]最大连续子序列和
- 最大子序列和:一道题窥探分治与动态规划
- ACM学习历程—HDU1003 Max Sum(dp && 最大子序列和)
- hud1151 动态规划 最大的公共子序列
- HDU 1087 Super Jumping! Jumping! Jumping![动态规划——最大递增子序列]
- 最大连续子序列和-动态规划
- 九度OJ 1077 最大子序列和 (动态规划)
- HDOJ 1087 求最大递增子序列的和(动态规划)
- HDU - 1231 最大连续子序列(动态规划)
- max sum(动态规划求最大连续子序列)
- DP 动态规划 Problem E 1005 最大递增子序列
- 求最大子序列和(动态规划或暴力)
- HDU 1087(动态规划-最大上升序列)
- 九度OJ 1501 最大连续子序列乘积 -- 动态规划
- 最大递增子序列---动态规划
- 动态规划回顾(1):最大连续子序列和