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

    Max Sum

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


    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
     
     
    题解:最简单的尺取法     一次遍历 
    一路加上去   如果累加的值sum    sum小于0了    就记录开头的节点
    如果sum大于了记录最大值的ans   就更新ans的值  和开头结尾
     1 //尺取法 
     2 
     3 #include<stdio.h>
     4 #include<string.h>
     5 int que[100002];
     6 main()
     7 {
     8   int t,flag;
     9   scanf("%d",&t);
    10   flag=t;
    11   while(t--)
    12   {
    13     memset(que,0,sizeof(que));
    14     int n,i,temp=0;
    15     int fir=1,end=1,sum=-1,max=-0x7fffffff;/*0x7fffffff这是2进制的int最大*/
    16     scanf("%d",&n);
    17     for(i=1;i<=n;i++)
    18       scanf("%d",&que[i]);
    19     for(i=1;i<=n;i++)
    20     {
    21       if(sum<0)
    22       {
    23         temp=i;
    24         sum=que[i];
    25         
    26       }
    27       else
    28       {
    29           sum+=que[i];
    30           
    31       }
    32       if(sum>max)
    33         {
    34              max=sum;
    35             fir=temp;
    36             end=i; 
    37         }
    38          
    39     }
    40     
    41     printf("Case %d:
    ",flag-t);
    42     printf("%d %d %d
    ",max,fir,end);
    43     if(t)
    44         printf("
    ");
    45   }
    46   return 0;
    47 }
  • 相关阅读:
    Oracle 11g导出空表
    如何破解ROS路由器禁用路由PPPOE拨号?
    Oracle入门心得(2)
    ORACLE了局,每天10问(五)
    若何消除Oracle数据库的安静隐患(3)
    Oracle入门心得(1)
    ORACLE用户自定义备份与恢复条记
    ORACLE成绩,每天10问(三)
    ORACLE问题,每天10问(一)
    怎样消除Oracle数据库的安全隐患(1)
  • 原文地址:https://www.cnblogs.com/52why/p/7478049.html
Copyright © 2011-2022 走看看