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): 242...

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 }

 

目录
相关文章
|
12月前
|
机器学习/深度学习 前端开发 JavaScript
230+本图灵编程高清文字版无水印电子书合集【制作不易,点赞收藏❤️】
今日精选,200余本图灵出版的高质量编程电子书,覆盖编程、系统架构、算法及机器学习等热门领域,助你全面提升技术能力。无论你是初学者还是资深开发者,都能从中找到适合自己的学习资源,从《Python编程:从入门到实践》到《深度学习入门》,每一本书都将是你技术成长道路上的良师益友,帮助你在瞬息万变的技术浪潮中站稳脚跟,稳步前行。
429 2
|
SQL 存储 Ubuntu
打开general_log对性能的影响
打开general_log对性能的影响
1616 0
打开general_log对性能的影响
|
物联网 Java 数据格式
阿里云物联网消息透传设备端payLoad设置问题
由于低配置且资源受限,或者对网络流量有要求的设备,不适合直接构造JSON数据与物联网平台通信,可将原数据透传到物联网平台。本文主要针对文档中未对设备端payLoad的设置进行介绍,初次使用容易出错,结合官方示例对payLoad对象的处理进行介绍。
14877 0
阿里云物联网消息透传设备端payLoad设置问题
|
编解码
笔记本的常见分辨率
笔记本的常见分辨率
|
存储 Kubernetes NoSQL
Kubernetes在AliCloud上部署并优化MongoDB
Kubernetes, 阿里云, MongoDB, 优化
403 0
|
编译器
立创EDA一些基础操作
立创EDA一些基础操作
744 0
|
缓存 Java Maven
解决Maven导入依赖报红问题
解决Maven导入依赖报红问题
841 0
|
存储 弹性计算 Linux
登录阿里云,注册账号|学习笔记
快速学习登录阿里云,注册账号
登录阿里云,注册账号|学习笔记
mac启动terminal终端快捷键
mac启动terminal终端快捷键
324 0
|
监控 网络协议 NoSQL
不为人知的网络编程(十一):从底层入手,深度分析TCP连接耗时的秘密
TCP的开销到底有多大,能否进行量化。一条TCP连接的建立需要耗时延迟多少,是多少毫秒,还是多少微秒?能不能有一个哪怕是粗略的量化估计?
811 0
不为人知的网络编程(十一):从底层入手,深度分析TCP连接耗时的秘密