HDUOJ----1003 Max Sum
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 113031 Accepted Submission(s): 26130
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
Author
Ignatius.L
http://acm.hdu.edu.cn/showproblem.php?pid=1003
动态规划....求最大子段和;
代吗:
1 #include<iostream>
2 #include<vector>
3 using namespace std;
4 int main()
5 {
6 int t,n,i,k,st,en,maxn,sum,flag=1;
7 cin>>t;
8 while(t--)
9 {
10 cin>>n;
11 vector<int>arr(n);
12 for(i=0;i<n;i++)
13 cin>>arr[i];
14 st=en=sum=0;
15 for(i=0,k=0;i<n;i++)
16 {
17 sum+=arr[i];
18 if(i==0||sum>maxn)
19 {
20 maxn=sum;
21 en=i;
22 st=k;
23 }
24 if(sum<0)
25 {
26 sum=0;
27 k=i+1;
28 }
29 }
30 cout<<"Case "<<flag++<<":"<<endl;
31 cout<<maxn<<" "<<st+1<<" "<<en+1<<endl;
32 if(t!=0)cout<<endl;
33 }
34 return 0;
35 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法