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

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        int time = 1;
        while(t-->0){
            int m = sc.nextInt();
            int p1,p2=1;
            int n = sc.nextInt();
            int now=n;
            int max=n;
            int x=1;
            p1=1;
            for(int i=1;i<m;i++){
                n=sc.nextInt();
                if(n>n+now){
                    x=i+1;
                    now=n;
                }else{
                    now = n+now;
                }
                if(now>max){
                    max=now;
                    p1=x;
                    p2=i+1;
                }

            }

            System.out.println("Case "+time+":");
            time++;
            System.out.println(max+" "+p1+" "+p2);
            if(t!=0){
                System.out.println();
            }

        }

    }

}
时间: 2024-07-28 13:23:11

HDOJ 1003 Max Sum的相关文章

[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

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]

HDOJ 2071 Max Num

Problem Description There are some students in a class, Can you help teacher find the highest student . Input There are some cases. The first line contains an integer t, indicate the cases; Each case have an integer n ( 1 ≤ n ≤ 100 ) , followed n stu

HDOJ 2058 The sum problem

Problem Description Given a sequence 1,2,3,--N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M. Input Input contains multiple test cases. each case contains two integers N, M( 1 <= N, M <= 1000000000).i

【DP专辑】ACM动态规划总结

转载请注明出处,谢谢.   http://blog.csdn.net/cc_again?viewmode=list          ----------  Accagain  2014年5月15日 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. 本人动态规划博客地址:http://blog.csdn.net/cc_again/article/category/1261899 ******************

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