最大子段和

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. hdfs namenode -initializeSharedEdits 和 hdfs namenode -bootstrapStandby

    hdfs namenode -initializeSharedEdits 将所有journal node的元文件的VERSION文件的参数修改成与namenode的元数据相同 hdfs namenod ...

  2. CTEX里的函数、符号及特殊字符

    CTEX里的函数.符号及特殊字符 声调 语法 效果 语法 效果 语法 效果 \bar{a}   \acute{a}   \check{a}   \grave{a}   \tilde{a}   \hat ...

  3. Redis常用的基本命令整理

    SET key value [EX seconds] [PX milliseconds] [NX|XX] 设置缓存 K-V,如果 key 已经存在,则重写 EX seconds -- 设置过期时间, ...

  4. Lua 栈中元素的位置

    Lua与C.C#等的交互是通过栈来实现的,每次插入元素都是放在栈顶(top),至于元素的index,可以使用正数和负数两种方式, 如取栈底开始至第index个元素 -index = gettop - ...

  5. eclipse导入git项目(转)

    1.首先在github.com上申请一个账号2.Eclipse需要安装egit插件,在Eclipse中选择help->Marketplace,在search中输入egit,找到后安装即可 3.从 ...

  6. 5.5 Selenium2中的元素定位

    WebDriver的更加面向对象的方式大大降低了Selenium的入门门槛,对Web元素的操作也非常之简单易学.实际项目用起来,工作量最大的部分就是你如何解析定位到你的目标项目页面中的各种元素.好比你 ...

  7. C#运算除法和求整

    在C#与法中,“/”除后所得的值的类型,跟他的除数和被除数的类型有关.如:                        int a=4;                        int b=5 ...

  8. Hadoop运维操作

    1.      处理hadoop的namenode宕机 处理措施: 进入hadoop的bin目录,重启namenode服务 操作命令: cd path/to/hadoop/bin ./hadoop-d ...

  9. 使用cnblogs.com的用户体验和提出来的建议

    1.是否提供良好的体验给用户(同时提供价值)? 我是很久以前就有CN的账号了的,因为这个网站有很多有用的信息,和比较活跃的论坛.很多不懂的问题可以求助上面的大神.但是,我第一次登陆主页面的时候我是懵逼 ...

  10. arrow css

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...