HDOJ-1003 Max Sum

简介: Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem ...

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

最大子序和问题。不过是要多存序列开头和结尾的位置。一次AC看代码吧。

//46MS  2068K
#include<iostream>
#include<cstdio>
using namespace std;
#define maxn 100005
int main(){
    int T,j=1;
    scanf("%d",&T);
    while(T--){
        int N,S[maxn];
        scanf("%d",&N);
        for(int i=1;i<=N;i++){
            scanf("%d",&S[i]);
        }
        int sum=0,max=-1005,first=0,last=0,temp=1;
        for(int i=1;i<=N;i++){
            sum+=S[i];
            if(sum>max){
                max=sum;
                first=temp;
                last=i;
            }
            if(sum<0){
                sum=0;
                temp=i+1;
            }
        }
        printf("Case %d:\n%d %d %d\n",j++,max,first,last);
        if(T!=0)
            printf("\n");
    }
    return 0;
}
目录
相关文章
|
消息中间件 存储 分布式计算
【Flume】Flume配置文件详细分析
【4月更文挑战第4天】【Flume】Flume配置文件详细分析
|
NoSQL 安全 Linux
【Redis入门】在阿里云上快速安装 Redis
如果你最近打算学习 redis 并且买了阿里云的 Linux 服务器,那么借助阿里云服务器和宝塔Linux面板,只需要简单几步就可以安装好 redis。
1415 0
【Redis入门】在阿里云上快速安装 Redis
|
JSON 算法 vr&ar
目标检测笔记(五):查看通过COCOEvaluator生成的coco_instances_results.json文件的详细检测信息,包含AP、AR、MR和DR等
本文介绍了如何使用COCO评估器通过Detectron2库对目标检测模型进行性能评估,生成coco_instances_results.json文件,并利用pycocotools解析该文件以计算AP、AR、MR和DR等关键指标。
1065 1
目标检测笔记(五):查看通过COCOEvaluator生成的coco_instances_results.json文件的详细检测信息,包含AP、AR、MR和DR等
|
运维 图形学 Python
从零开始的PICO教程(2)--实时预览应用场景
这篇文章是关于如何使用PICO Unity Live Preview Plugin在PICO设备上进行实时预览应用场景的教程,包括准备工作、操作步骤和故障排查方法。
Cause: java.sql.SQLIntegrityConstraintViolationException: Column ‘id‘ in field list is ambiguous
Cause: java.sql.SQLIntegrityConstraintViolationException: Column ‘id‘ in field list is ambiguous
629 0
|
机器学习/深度学习 消息中间件 缓存
栈与队列的实现
栈与队列的实现
|
存储 API 持续交付
virsh 的工作原理
`virsh`是基于libvirt API的命令行虚拟机管理工具,适用于自动化部署和管理。它提供交互和非交互模式,用于连接到Hypervisor,执行如创建、管理、配置虚拟机及网络、存储等任务。用户需相应权限,无权限者只能读取信息。virsh通过与libvirtd守护进程通信,实现对虚拟化环境的控制,是系统管理员的强大助手。
422 2
|
Java Apache Android开发
超越传统:用Java的OSGi框架构建灵活模块化应用
超越传统:用Java的OSGi框架构建灵活模块化应用
661 0
|
JavaScript 数据处理 Python
nodejs | 看看豆瓣Top250电影有哪些?
前面写了`Python` 的版本,然后用 `nodejs` 页写一个吧!
299 0
|
Linux
Linux面试常用命令大全(常用指令)
Linux面试常用命令大全(常用指令)
191 0