zoukankan      html  css  js  c++  java
  • HDU1003

    Max Sum

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

    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.找到maxsum a[i] = max(maxsum a[i-1]+a[i],a[i])  这个规律。
    2.注意在s<0情况下的处理。
    3.DP的思路。
     1 #include<iostream>
     2 #include<cstdio>
     3 int max(int x,int y )
     4 {
     5     if (x>y)
     6         return x;
     7     else
     8         return y;
     9 }
    10 int main()
    11 {
    12     int T,n,a,s,sum,x,y,z;
    13     while (~scanf("%d",&T))
    14     {
    15         for (int o=1;o<=T;o++)
    16         {
    17             scanf("%d",&n);
    18             z=1;s=0;sum=-1001;x=1,y=1;
    19             for (int p=1;p<=n;p++)
    20             {
    21                 scanf("%d",&a);
    22                 s+=a;
    23                 if(s>sum)
    24                 {
    25                     sum=s;
    26                     x=z;
    27                     y=p;
    28                 }
    29                 if(s<0)
    30                 {
    31                     s=0;
    32                     z=p+1;
    33                 }
    34             }
    35             printf("Case %d:
    ",o);
    36             printf("%d %d %d
    ",sum,x,y);
    37             if(o!=T)
    38                 puts("");
    39         }
    40     }
    41     return 0;
    42 }
    View Code
  • 相关阅读:
    jdbc的数据库驱动类DriverManager.getConnection()详解
    BootStrap_table.js 学习
    Oracle 练习
    Oracle游标
    Oracle流程控制语句
    Oracle定义变量、常量
    Oracle中的数据类型
    初识Oracle中的正则表达式
    Oracle primary key&foreign key
    oracle Extract 函数
  • 原文地址:https://www.cnblogs.com/xfjy/p/4982288.html
Copyright © 2011-2022 走看看