zoj1003-Max Sum (最大连续子序列之和)

http://acm.hdu.edu.cn/showproblem.php?pid=1003

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 161361    Accepted Submission(s): 37794

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

代码:

 1 #include <fstream>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <cmath>
 7 #include <cstdlib>
 8
 9 using namespace std;
10
11 #define EPS 1e-10
12 #define ll long long
13 #define INF 0x7fffffff
14
15 int main()
16 {
17     //freopen("D:\\input.in","r",stdin);
18     //freopen("D:\\output.out","w",stdout);
19     int T,n,ans,tn,l,r,al,ar,t;
20     scanf("%d",&T);
21     for(int tt=1;tt<=T;tt++){
22         scanf("%d",&n);
23         ans=tn=-INF;
24         for(int i=1;i<=n;i++){
25             scanf("%d",&t);
26             if(tn<0){
27                 l=r=i;
28                 tn=t;
29             }else{
30                 tn+=t;
31                 r=i;
32             }
33             if(tn>ans){
34                 al=l;
35                 ar=r;
36                 ans=tn;
37             }
38         }
39         printf("Case %d:\n%d %d %d\n",tt,ans,al,ar);
40         if(tt!=T)   puts("");
41     }
42     return 0;
43 }

时间: 2024-10-13 15:40:28

zoj1003-Max Sum (最大连续子序列之和)的相关文章

HDU 1003 Max Sum 最大连续子序列的和

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

杭电1003 Max Sum 【连续子序列求最大和】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目意思: 即给出一串数据,求连续的子序列的最大和 解题思路: 因为我们很容易想到用一个max来存放找到的子序列的和中的最大值,通过不断比较,对max的值进行更新,最后我们就能够得到最大子序列的和,于是很容易想到用暴力搜索,见上一篇博客,这样的时间复杂度为O(n^3),是超时的. 又因为想到只要一个数不是负数,不管它再小,加上去也是会使和变大的,所以我们需要用另一个变量来判断即将要加上的一个

hdu1003 Max Sum【最大连续子序列之和】

题目链接:https://vjudge.net/problem/HDU-1003 题目大意:给出一段序列,求出最大连续子序列之和,以及给出这段子序列的起点和终点. 解题思路:最长连续子序列之和问题其实有很多种求解方式,这里是用时间复杂度为O(n)的动态规划来求解. 思路很清晰,用dp数组来表示前i项的最大连续子序列之和,如果dp[i-1]>=0的话,则dp[i]加上dp[i-1]能够使dp[i]增大:若dp[i-1]<0的话,则重新以dp[i]为起点,起点更新. #include <cs

最大连续子序列之和,最大连续子序列乘积

最大连续子序列之和问题描述为:数组中里有正数也有负数,连续的一个或多个整数组成一个子数组,每个子数组都有一个和,求所有子数组的和的最大值.分析,对数组a进行一遍扫描,sum[i] 为前i个元素中,包含第i个元素且和最大的连续子数组,MaxSum保存当前子数组中最大和,对于a[i+1]来说,sum[i+1] = sum[i]+a[i+1],此时如果sum[i+1]<0,那么sum需要重新赋0,从i+1之后开始累加,如果sum[i+1]>0,那么MaxSum = max(MaxSum, Sum[i

[CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大

17.8 You are given an array of integers (both positive and negative). Find the contiguous sequence with the largest sum. Return the sum. LeetCode上的原题,请参见我之前的博客Maximum Subarray. 解法一: int get_max_sum(vector<int> nums) { int res = INT_MIN, sum = INT_MI

算法面试题 之 最长连续子序列之和

参考 http://www.ahathinking.com/archives/120.html var arr = [2, 8,-2, 3, 5, -3, 2]; //传统方法 O(n^2) function fun1(arr){ var maxSum =arr[0]; var maxSumArr = []; for(var i=0; i< arr.length; i++){ var sum = arr[i]; var sumArr = [arr[i]]; for(j=i+1; j<arr.l

[POJ1050]To the Max (矩阵,最大连续子序列和)

数据弱,暴力过 题意 N^N的矩阵,求最大子矩阵和 思路 悬线?不需要.暴力+前缀和过 代码 //poj1050 //n^4暴力 #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #define N 105 #define INF 0x3fffffff using namespace std; int a[N][N]; int sum[N]; int ans; int

最大连续子序列和

对于给定的数组 numnum,一个长度为 ss 的连续子序列是指由 num_i,num_{i+1},num_{i+2}\ldots,num_{i+s-1}num?i??,num?i+1??,num?i+2??…,num?i+s−1?? 组成的序列.数组中的元素有可能为正数.负数或 00.你需要从数组中找出元素总和最大的一个连续子序列. 比如,对于数组 1,-3,2,6,-5,81,−3,2,6,−5,8,其最大连续子序列之和是 2+6-5+8=112+6−5+8=11. 对于一段区间内的最大连续

求解最大连续子序列和问题

解法1:maxSubSum1(a,n)算法中用三重循环来穷举所有的连续子序列,计算它们的和,时间复杂度为T(n) = O(n^3). 1 long maxSubSum1(int a[],int n) 2 { 3 int i,j,k; 4 long maxSum = a[0],thisSum; 5 for(i = 0;i < n;i++) 6 { 7 for(j = i;j < n;j++)//两重循环穷举所有连续子序列 8 { 9 thisSum = 0; 10 for(k = i;k <