HDU1003--Max Sum--最大连续和
来源:互联网 发布:java程序员的发展前景 编辑:程序博客网 时间:2024/05/28 15:06
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
#include <iostream>#include <cstdio>using namespace std;int A[100008];#define INF 0x3fint maxmax(int a,int b){return a>b?a:b;}int main(){int t;scanf("%d",&t);for(int i=1;i<=t;i++){int n;scanf("%d",&n);for(int j=1;j<=n;j++){scanf("%d",&A[j]);}int sum=0,max=-INF;int head=1,tail=1;for(int j=1;j<=n;j++){sum+=A[j];if(max<sum){tail=j;max=sum;}sum=sum<0?0:sum;}int sum1=0;for(int j=1;j<=n;j++){sum1+=A[j];if(sum1==max){break;}if(sum1<0){sum1=0;head=j+1;}}if(i!=t)cout<<"Case "<<i<<":"<<endl<<max<<" "<<head<<" "<<tail<<endl<<endl;else cout<<"Case "<<i<<":"<<endl<<max<<" "<<head<<" "<<tail<<endl;}return 0;}
- HDU1003--Max Sum--最大连续和
- Max Sum(hdu1003最大连续子串和+分治法)
- hdu1003——max sum;(最大连续字串和)
- HDU1003 Max Sum(最大连续子段和)
- hdu1003 Max Sum (求连续子区间最大和)
- HDU1003 - Max Sum (最大连续子串和)
- HDU1003 Max Sum(最大连续子序和、贪心、DP)
- Max Sum(HDU1003)最大连续子序列和
- hdu1003 Max Sum 最大连续子序列
- Max Sum hdu1003 最大子段和
- HDU1003 Max Sum(求最大字段和)
- hdu1003 Max Sum求最大和区间
- hdu1003 Max sum&hdu1231 最大连续上升子序列
- hdu1003 Max Sum (求连续的和最大的自链
- hdu1003 连续最大和
- ACMSTEP 3.2.1 && HDU1003 Max Sum //DP 最大字段和
- hdu1003 Max Sum(DP之最大子序列和)
- hdu1003 Max Sum 最大子列和问题
- Latex中的一些命令
- selinux android max_permissions.xml seapp_contexts 关系。
- 网络KTV中的技术模式的相关介绍
- 对作业中进程一些操作
- HDU_2021 发工资咯:)
- HDU1003--Max Sum--最大连续和
- 如何在VC6.0下用pthread.h这个头文件
- 排序二叉树 生成双向链表
- select 函数----续 3
- 数据项目面临的六大挑战
- 使用复选框控制表格中的文本框
- 前台用的是ajax请求,后台json字符串拼接方式
- HDU2084--动态规划数塔
- (转)iOS编程——Objective-C KVO/KVC机制