最大子段和

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. Android开发把项目打包成apk

    做完一个Android项目之后,如何才能把项目发布到Internet上供别人使用呢?我们需要将自己的程序打包成Android安装包文件--APK(Android Package),其后缀名为" ...

  2. mysql报关于用户密码1045(28000),几种处理方法 (zhuan)

    http://blog.itpub.net/29371470/viewspace-1409075/ http://blog.csdn.net/rosten/article/details/250658 ...

  3. java里的基本数据类型

    java里一共有八大数据类型 boolean(未定) char(2字节) byte(1字节) short(2字节) int(4字节) long(8字节) float(4字节) double(8字节), ...

  4. 能源项目xml文件标签释义--DefaultAdvisorAutoProxyCreator

    [Spring]AOP拦截-三种方式实现自动代理 这里的自动代理,我讲的是自动代理bean对象,其实就是在xml中让我们不用配置代理工厂,也就是不用配置class为org.springframewor ...

  5. [saiku] 登陆/选择cube 时发生了什么

    一 登陆saiku时发生了什么 大致流程 saiku 默认有admin和两个开发人员的身份 登陆saiku时,后台用户认证成功后创建了用户信息session 并在session中存储了随机生成的一个s ...

  6. 《Java程序设计》第三周学习总结

    20145224-陈颢文 <Java程序设计>第三周学习总结 教材学习内容总结 一.定义类: ·类定义时使用class关键字,要对类中变量(值域成员/对象数据成员)行类型声明. class ...

  7. WMI执行远程文件(RPC)

    通过wmi在远程机上执行命令(wmi:windows management interface 可以通过一个公共的接口访问不同操作系统(windows系统)的构成单元,利用它可以高效的管理远程和本地的 ...

  8. NT内存

    在NT/2K/XP中,操作系统利用虚拟内存管理技术来维护地址空间映像,每个进程分配一个4GB的虚拟地址空间.运行在用户态的应用程序,不能直接访问物理内存地址:而运行在核心态的驱动程序,能将虚拟地址空间 ...

  9. SSH基础(2)

    linux下配置 ssh运行的参数详解:

  10. JavaScript实现五子棋的界面设计

    五子棋的界面设计包括绘制棋盘.绘制棋子及黑白棋轮流落子. 涉及的知识点主要有canvas绘制直线.设置画笔颜色:canvas画圆.填充渐变色 1.绘制棋盘 先设定棋盘的宽.高:然后利用for循环,根据 ...