HDU 1003 Max sum
来源:互联网 发布:北风网java视频教程 编辑:程序博客网 时间:2024/06/05 19:32
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 159198 Accepted Submission(s): 37255
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
Author
Ignatius.L
解题思路:
这道题,看起来很水,也就是个dp求最大连续子序列的和。。那么只要定义好状态就可以了,dp[i] 表示的是以a[i]为结尾的最大子序列的和。
那么状态转移方程为:dp[i] = max( dp[i-1]+a[i],a[i] )
不说了,直接上代码:
# include<cstdio># include<iostream>using namespace std;# define MAX 100000int mat[MAX];struct node{ int l; int r;}seg[MAX];int main(void){ int n; int icase = 0; int t;cin>>t; while ( t-- ) { icase++; cin>>n; for ( int i = 1;i <= n;i++ ) { cin>>mat[i]; } seg[1].l = 1; seg[1].r = 1; for ( int i = 2;i <= n;i++ ) { if ( mat[i] <= mat[i-1]+mat[i] ) { mat[i] = mat[i-1]+mat[i]; seg[i].l = seg[i-1].l; seg[i].r = i; } else { seg[i].l = seg[i].r = i; } } int maxID = 1; int _max = mat[1]; for ( int i = 2;i <= n;i++ ) { if ( mat[i] > _max ) { _max = mat[i]; maxID = i; } } printf("Case %d:\n",icase); printf("%d %d %d\n",_max,seg[maxID].l,seg[maxID].r); if ( t != 0 ) cout<<endl; } return 0;}
0 0
- 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
- 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
- HDU-1003-Max Sum
- hdu - 1003 - Max Sum
- Asp.Net约束注册用户名密码规则
- Oracle 闪回特性(FLASHBACK DATABASE)
- 如何判断当前的上下文是否是一个列表
- Labview与其他应用程序的接口设计
- android PackagerManangeServicer 启动过程
- HDU 1003 Max sum
- NFS读写块大小问题分析
- Zookeeper开源客户端框架Curator简介
- NetFilter (1)
- getopt
- Windows主机:Apache启用rewrite和.htaccess
- V8之内存管理(一)
- Driver Structure
- ResourceBundle读取properties文件 代码片段