[ACMcoder] 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 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

解题思路

动态规划

实现代码

#include <iostream>
using namespace std;

int main()
{
    int caseCount, numCount;
    int caseIndex, numIndex;
    cin>>caseCount;
    for (caseIndex = 1; caseIndex <= caseCount; caseIndex++)
    {
        cin>>numCount;
        int num;
        int local_max = 0;
        int global_max = 0x80000000;
        int start, end;
        int temp = 1;
        for (numIndex = 1; numIndex <= numCount; numIndex++)
        {
            cin>>num;
            local_max += num;
            if (local_max > global_max)
            {
                global_max = local_max;
                start = temp;
                end = numIndex;
            }

            if (local_max < 0)
            {
                local_max = 0;
                temp = numIndex + 1;
            }
        }

        cout<<"Case "<<caseIndex<<":"<<endl;
        cout<<global_max<<" "<<start<<" "<<end<<endl;
        if (caseIndex != caseCount)
            cout<<endl;
    }
}
时间: 2024-10-26 15:32:53

[ACMcoder] Max Sum的相关文章

HDOJ 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 an

Max Sum 杭电 1003

又是凌晨两三点,这也许就是程序员的诟病.当看到这道题的时候,脑袋里面出现的就是,动态规划,大家不要笑我.我也是个菜鸟,毕竟大家都是在不断地学习中. 题目的意思是给你一个数列,找到一个子数列,这个子数列的和是所有子数列中和最大的. 当然把数列的所有数都列出来肯定不现实. 黑黑,不知道正不正确,我是先从第一个数开始依次加到最后一个数,并且把每一次加的和存到对应的一个和数组中.这样得到了一个不间断的和的数列,但是这样显然还是不行,在加的时候你还得判断一下,当和小于零的时候,就得把起始位置,向后挪一个了

max sum of sequence(序列最大和)

#include <iostream> using namespace std; int main(int argc, char *argv[]){    int n;     cin >> n;    const int maxlen = n;     int s[maxlen];    int arr[maxlen][maxlen];        for (int i = 0; i < maxlen; i++)    {        cin >> s[i]

PostgreSQL 流式统计 - insert on conflict 实现 流式 UV(distinct), min, max, avg, sum, count ...

标签 PostgreSQL , 流式统计 , insert on conflict , count , avg , min , max , sum 背景 流式统计count, avg, min, max, sum等是一个比较有意思的场景,可用于实时大屏,实时绘制统计图表. 比如菜鸟.淘宝.阿里游戏.以及其他业务系统的FEED日志,按各个维度实时统计输出结果.(实时FEED统计,实时各维度在线人数等) PostgreSQL insert on conflict语法以及rule, trigger的功

UVa 10827:Maximum sum on a torus

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1768 原题: A grid that wraps both horizontally and vertically is called a torus. Given a torus where each cell contains an inte

【最大子矩阵和】HDU1081-To The Max

To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8193    Accepted Submission(s): 3981 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectan

专题一——Swift2.2语言预览

专题一--Swift2.2语言预览 一.引言         本系列专题是我通过阅读Swift2.2语言开发文档,翻译总结加上自己的理解整理而成.其中大部分结构和内容都来自开发文档,有疏漏和错误之处,还望更多朋友指出,共同交流进步,我的QQ:316045346. 二.从HelloWorld开始         在学习很多编程语言时,都是从HelloWorld入门,下面代码就是一个完整的HelloWorld程序: ? 1 print("Hello, World!") 分析上面代码,可以发

MySQL 文件系统

mysql 实际上,这不是通常意义上的文件系统,它没有磁盘空间, 而是使用MySQL 守护程序来存储数据.可以把SQL 表和 一些函数通过文件系统来实现. 一.怎样实现? 让我们来看使用实例: [root@localhost /root]# mount -t corbafs -o `cat /tmp/mysqlcorbafs.ior` none /mnt/mysql/ [root@localhost /root]# mount /dev/hda3 on / type ext2 (rw) none

MySQL的SELECT语句技巧

以下的文章主要描述的是MySQL SELECT使用技巧大全,MySQL SELECT在实际中的应用比例还是占为多数的,如果你对这一新开发的技术,心存好奇的话,以下的文章将会揭开它的神秘面纱. 记录一些select的技巧: 1.select语句可以用回车分隔 $sql="select * from article where id=1" 和 $sql="select * from article where id=1" ,都可以得到正确的结果,但有时分开写或许能更明了