zoukankan      html  css  js  c++  java
  • HDU 1003

    Max Sum

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


    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
    View Code
    //第二组错误,输出了 7 7
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    const int maxn = 100010;
    
    int ans[maxn] = {0};
    int b[maxn][2] = {0};
    int a[maxn];
    
    int main()
    {
        int i,j,k;
        int T;
        scanf("%d",&T);
        int n,m;
        int num;
        int flag = 1;
        int temp = T;
        while(T--)
        {
            scanf("%d",&num);
            memset(ans,0,sizeof(ans));
            memset(b,0,sizeof(b));
            memset(a,0,sizeof(a));
    
    
            for(i=1; i<=num; i++)
                scanf("%d",a+i);
            ans[1] = a[1];
            b[1][0] = b[1][1] = 1;
            int max = -1;
            int next;
            for(i=2; i<=num; i++)
            {
                if(ans[i-1]>0)
                {
                    ans[i] = ans[i-1] + a[i];
                    //if(a[i]>0)
                    {
                        b[i][0] = b[i-1][0];
                        b[i][1] = i;
                    }
                   // else
                   // {
                   //     b[i][0] = b[i-1][0];
                   //     b[i][1] = i-1;
                   // }
                }
                else
                {
                    ans[i] = a[i];
                    b[i][0] = b[i][1] = i;
                }
                if(ans[i]>max)
                {
                    max = ans[i];
                    next = i;
                }
    
            }
            printf("Case %d:\n",flag);
            printf("%d %d %d\n",max,b[next][0],b[next][1]);
            if(flag<temp)
                printf("\n");
            flag++;
        }
        return 0;
    }
     1 #include <cstdio>
     2 #include <cstring>
     3 using namespace std;
     4 
     5 const int maxn = 100010;
     6 int a[maxn] = {0};
     7 
     8 int main()
     9 {
    10     int i,j,k;
    11     int T;
    12     scanf("%d",&T);
    13     int num;
    14     int flag = 1;
    15     int sum = 0;
    16     int start,end,last;
    17     int temp = T;
    18     while(T--)
    19     {
    20         
    21         int max = (1<<31);
    22         scanf("%d",&num);
    23         memset(a,0,sizeof(a));
    24         sum = 0;
    25         last = 1;
    26         for(i=1; i<=num; i++)
    27         {
    28             scanf("%d",a+i);
    29             sum += a[i];
    30             if(sum>max)
    31             {
    32                 start = last;
    33                 max= sum;
    34                 end = i;
    35             }
    36             if(sum<0)
    37             {
    38                 sum = 0;
    39                 last = i+1;
    40             }
    41 
    42         }
    43         printf("Case %d:\n",flag);
    44         printf("%d %d %d\n",max,start,end);
    45         if(flag<temp)
    46             printf("\n");
    47         flag++;
    48     }
    49     while(1);
    50     return 0;
    51 }
  • 相关阅读:
    多测师讲解html _伪类选择器17_高级讲师肖sir
    多测师讲解html _后代选择器16_高级讲师肖sir
    多测师讲解html _组合选择器_高级讲师肖sir
    多测师讲解html _标签选择器14_高级讲师肖sir
    前端 HTML form表单标签 input标签 type属性 重置按钮 reset
    前端 HTML form表单标签 textarea标签 多行文本
    前端 HTML form表单标签 input标签 type属性 file 上传文件
    前端 HTML form表单标签 input标签 type属性 radio 单选框
    前端 HTML form表单标签 input标签 type属性 checkbox 多选框
    前端 HTML form表单目录
  • 原文地址:https://www.cnblogs.com/hxsyl/p/3059580.html
Copyright © 2011-2022 走看看