Max Sum

原创 2018年04月17日 19:54:59
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
 
#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int mod=1e9+7;
const int N=1000+5;

char s[N];
ll sum[N][N];

int main(){
   //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int sum,mm,p;
    int T;
    cin>>T;
    int z=1;
    while(sum=0,p=1,mm=1<<31,T--){
        if(z!=1)puts("");
        int n;
        cin>>n;
        int pl,pr;
        for(int i=1;i<=n;i++){
            int t;
            scanf("%d",&t);
            sum+=t;
            if(sum>mm){
                mm=sum;
                pl=p;
                pr=i;
            }
            if(sum<0){
                sum=0;
                p=i+1;
            }
        }
        printf("Case %d:\n%d %d %d\n",z++,mm,pl,pr);
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ujn20161222/article/details/79979778

VueJS 2.x终极开发教程

一、 绝对诚意:23章节,21小时,357个视频 二、绝不只是理论:11个练习,3个工程实践穿插课程中 三、最新VueJS 2.X版本,视频会随着版本升级 四、覆盖单页应用和多页应用,从简单页面到复杂企业级应 欢迎学员加入我们VueJS学习QQ群:635858610,作业、练习、代码,通过群提供给学员
  • 2018年03月08日 16:16

Max Sum(最大子段和问题)

最大子段和:给定一个序列(元素可正可负),找出其子序列中和最大的值。 我们用dp(i)表示序列中以元素ans(i)结尾的序列的最大子段和,那么有:dp(i)=max(dp)...
  • AC_Gibson
  • AC_Gibson
  • 2014-12-04 16:54:51
  • 1559

动态规划——Hdu_1003_Max Sum

2012/7/22 11:04 Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...
  • code_pang
  • code_pang
  • 2012-07-22 12:32:41
  • 6248

Java - 杭电1003 Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submissio...
  • qq_34594236
  • qq_34594236
  • 2016-05-26 21:39:31
  • 864

hdu 1024 Max Sum Plus Plus(动态规划+m子段和的最大值)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Limit: 2000/1000 MS (J...
  • qiqi_skystar
  • qiqi_skystar
  • 2016-01-28 12:40:56
  • 3120

hdu Max Sum Plus Plus(最大m段子段和)

1、http://acm.hdu.edu.cn/showproblem.php?pid=1024 2、题目大意: 已知有n个数,求m段不相交的子段权值之和最大, 状态转移方程:dp[i][j]表...
  • sdjzping
  • sdjzping
  • 2013-03-07 12:09:36
  • 3657

HDU1003 Max Sum 最大子序列和的问题【四种算法分析+实现】

就拿杭电OJ上的第1003题开始吧,这题比原书要复杂一些。 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your ...
  • u012846486
  • u012846486
  • 2014-05-18 21:08:59
  • 1665

动态规划入门——Max Sum

转载请注明出处:http://blog.csdn.net/a1dark 分析:求最大子序列和的题、水题略过 #include int main(){ int t,n,x; in...
  • verticallimit
  • verticallimit
  • 2013-09-30 22:23:51
  • 893

363. Max Sum of Rectangle No Larger Than K

Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s...
  • wdlsjdl2
  • wdlsjdl2
  • 2016-07-14 11:09:13
  • 1240

最大m子段和问题 Max Sum Plus Plus —— 动态规划

最大m子段和问题 Max Sum Plus Plus —— 动态规划
  • mason_mow
  • mason_mow
  • 2014-05-31 23:31:23
  • 2734
收藏助手
不良信息举报
您举报文章:Max Sum
举报原因:
原因补充:

(最多只允许输入30个字)