zoukankan      html  css  js  c++  java
  • hdu 1003 Max Sum (DP)

    Max Sum

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


    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
     
    Recommend
    We have carefully selected several similar problems for you:  1421 1024 1505 2845 2577 
     
    简单dp思想:
     1 //15 MS    236 KB    Visual C++    
     2 #include<stdio.h>
     3 int main(void)
     4 {
     5     int t,n,k=1;
     6     int a;
     7     scanf("%d",&t);
     8     while(t--)
     9     {
    10         if(k>1) printf("
    ");
    11         scanf("%d",&n);
    12         int l=1,r=1,sum=-0x7ffffff,tmp=0,tl=1;
    13         for(int i=1;i<=n;i++){
    14             scanf("%d",&a);
    15             tmp+=a;
    16             if(tmp>sum){
    17                 sum=tmp;
    18                 l=tl;
    19                 r=i;
    20             }
    21             if(tmp<0){
    22                 tmp=0;
    23                 tl=i+1;          
    24             }
    25         }
    26         printf("Case %d:
    ",k++);
    27         printf("%d %d %d
    ",sum,l,r);
    28     }
    29     return 0;
    30 }
    31 /*
    32 
    33 30
    34 3 -1 0 -1
    35 3 -1 0 1
    36 3 -1 -2 -3
    37 
    38 */
  • 相关阅读:
    AW245 你能回答这些问题吗(连续子段和线段树)
    AW256 最大异或和(可持久化0/1trie树)
    AW247 亚特兰蒂斯(区间覆盖线段树)
    P1616 疯狂的采药
    P1060 开心的金明
    AW252 树(点分治)
    AW250 磁力块(分块)
    php绘图(一)
    判断一个文件里面有多少各种格式的图片
    添加图片水印图标
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3683969.html
Copyright © 2011-2022 走看看