最大子段和

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 
 
题目大意:给出一串序列,要求输出其子序列的最大连续和与它所在的位置。
 
 
分析:由于时间限制为一秒且n=1e5,所用两个for循环肯定超时,不过可以动态规划,也可以二分。
 
 
 
代码如下:
 
 
 
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{ int i,j,kase=0,n,t,b,c,d,x,sum;
scanf("%d",&t);
while(t--)
{ scanf("%d",&n);
for(j=1;j<=n;j++)
{
scanf("%d",&x);
if(j==1)
{
sum=b=x;
i=c=d=1;
}
else
{
if(x>x+b)
{
b=x;
i=j;
}
else
b+=x;
}
if(b>sum)
{
sum=b;
c=i;
d=j;
}
}
printf("Case %d:\n",++kase);
printf("%d %d %d\n",sum,c,d);
if(t)
cout<<endl; }
return 0;
}
 

最大子段和-Program A的更多相关文章

  1. GSS1 spoj 1043 Can you answer these queries I 最大子段和

    今天下午不知道要做什么,那就把gss系列的线段树刷一下吧. Can you answer these queries I 题目:给出一个数列,询问区间[l,r]的最大子段和 分析: 线段树简单区间操作 ...

  2. C语言程序设计100例之(13):最大子段和

    例13        最大子段和 题目描述 给出一段序列,选出其中连续且非空的一段使得这段和最大.例如在序列2,-4,3,-1,2,-4,3中,最大的子段和为4,该子段为3,-1,2. 输入格式 第一 ...

  3. 最大子段和(c++)

    // 最大子段和.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namesp ...

  4. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  5. Solved: “Cannot execute a program. The command being executed was \roslyn\csc.exe”

    When you publish your ASP.NET project to a hosting account such as GoDaddy, you may run into the iss ...

  6. 关于The C compiler "arm-none-eabi-gcc" is not able to compile a simple test program. 的错误自省...

    在 GCC ARM Embedded https://launchpad.net/gcc-arm-embedded/ 上面下载了个arm-none-eabi-gcc 用cmake 编译时 #指定C交叉 ...

  7. c中使用gets() 提示warning: this program uses gets(), which is unsafe.

    今天在C代码中使用gets()时提示“warning: this program uses gets(), which is unsafe.”,然后这个程序还能运行,无聊的我开始查阅资料,为啥gets ...

  8. Unable to load R3 module D:\Program Files\Oracle\VirtualBox/VBoxDD.DLL (VBoxDD): GetLastError=1790 (VERR_UNRESOLVED_ERROR).

    Unable to load R3 module D:\Program Files\Oracle\VirtualBox/VBoxDD.DLL (VBoxDD): GetLastError=1790 ( ...

  9. Git for Windows - The Program can't start because libiconv2.dll is missing

    今天在新装的win10 预览版上面,发现git不能启动了,提示信息主要是: The Program can't start because libiconv2.dll is missing 于是我在网 ...

随机推荐

  1. gO语言的安装和环境变量的配置

    一.Go语言下载 go语言官方下载地址:https://golang.org/dl/ 找到适合你系统的版本下载,本人下载的是windows版本.也可以下载Source自己更深层次研究go语言. 二.G ...

  2. Bootstrap强调相关的类

    在Bootstrap中除了使用标签<strong>.<em>等说明正文某些字词.句子的重要性,Bootstrap还定义了一套类名,这里称其为强调类名(类似前面说的“.lead” ...

  3. aliyun CentOS6.5 上 svn 安装笔记

    为了方便管理自己的一些学习资料.总结等,在CentOS6.5上安装SVN,记录过程如下: 1.1  安装方式  独立服务器  fsfs SVN服务器有2种运行方式:独立安装.SVN+Apache.独立 ...

  4. c++11 其他特性(一)

    c++11还增加了许多有用的特性,比如: 1. 委托构造函数 如果一个类含有很多构造函数,这些构造函数有一些重复的地方,比如: class A{ public: A(){}; A(int a){ a_ ...

  5. Hacker Technology

    扒一扒「黑客军团」中用到的黑客工具 黑客 (Hacker) - 知乎 nmap - 百科 NMAP - 官网  中文翻译 Havij sqlmap 熊猫烧香 懒人在思考 零基础如何学习 Web 安全? ...

  6. Ramdisk文件系统无法启动

    当kernel是使用ramdisk时,bootm命令有两种使用方式: 1.bootm ${kernel_addr} 此种方式要求bootargs变量包含ramdisk的地址和大小,ramdisk的格式 ...

  7. xml中的xmlns,xmlns:xsi,xsi:schemaLocation有什么作用,如果没有会怎么样呢

    如 maven 的 pom.xml 开头是下面这样的谁能解释下,这东西有社么用,不写这东西又会怎么样的,官方拷贝来的说明文档就算了,我想要简明扼要的说明.不胜感激---------<projec ...

  8. spring来了-03-bean创建细节

    对象创建:单例/多例  [bean节点的属性scope] scope="singleton", 默认值,即默认是单例     [service/dao/工具类] scope=&qu ...

  9. Disable SELinux CentOS 7

    Disable SELinux CentOS 7 This blog covers the basic steps to disable SELinux on CentOS 7 first we ne ...

  10. 《javascript高级程序设计》第四章 Variables,scope,and memory

    4.1 基本类型和引用类型的值 primitive and reference values 4.1.1 动态的属性 dynamic properties 4.1.2 复制变量值 copying va ...