Max Sum **

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

正确代码

#include<iostream>
#include<cstdio>
using namespace std;
int a[200];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
int t,resmax=0,sursum=0,curleft=1,temp=0,resleft=0,resright,ressum=0;
cin>>t;
for(int i=1;i<=t;i++)
{
cin>>temp;
sursum+=temp;
if(sursum>resmax)
{
resmax=sursum;
resright=i;
resleft=curleft;
}
if(sursum<0)
{
sursum=0;
curleft=i+1;
}
}
printf("Case %d:\n%d %d %d\n",i, resmax, resleft, resright);
printf(i==t?"":"\n");
}
}
}

题意理解

题意是按照顺序进行不停的相加,假设每一次加一个数所得到的数据都另外储存在一个数组里面,则到最后进行每一步加法的大小比较,输出三组数据,第一个数据是不停相加过程中出现的最大值,第二个数据是相加过程中的起始点,注意,这里相加时若出现负数可以进行清零,然后起始点赋值成变成负值位置的下一个位置。假设中的数据实际不会用到只是为了方便解释题意,用数组进行储存每一步的相加数据太麻烦。第三个数据则是相加过程中的结束点。

对于最大值的判断:如何判断最大值是完成这个题目的首要问题,则需要一个累加的代码进行不断相加输入的数据,还需要一个判断程序,即if条件语句,进行判断是否大于“历届”最大值,最后需要一个空间储存最大值。首先定义一个空间resmax,进行初始化为零,接着对不停累加的变量sursum进行比较,与resmax进行比较,当前值大于结果值时更新resmax, resleft, resright,当前值小于0时更新curleft, cursum;。

#C++初学记录(ACM试题2)的更多相关文章

  1. #C++初学记录(ACM试题1)

    A - Diverse Strings A string is called diverse if it contains consecutive (adjacent) letters of the ...

  2. #C++初学记录ACM补题(D. Candies!)前缀和运算。

    D - Candies!   Consider a sequence of digits of length [a1,a2,-,a]. We perform the following operati ...

  3. #C++初学记录(acm试题#预处理)

    C - Lucky 7 in the Pocket BaoBao loves number 7 but hates number 4, so he refers to an integer as a ...

  4. #C++初学记录(set进阶#acm cf 190802 B. Subsegments)

    B. Subsegments#set进阶 Programmer Sasha has recently begun to study data structures. His coach Stas to ...

  5. #C++初学记录(sort函数)

    sort函数 前言:当进行贪心算法的学习时,需要用到sort函数,因为初学c++汇编语言,sort的具体用法没有深入学习,所以这里进行sort学习记录并只有基础用法并借用贪心算法题目的代码. 百度百科 ...

  6. 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始

    以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告

  7. javaweb初学记录

    原文 链接 http://blog.csdn.net/iojust/article/details/52429805 - ---热情依旧 - 环境搭建: - jdk环境配置 jdk下载: http:/ ...

  8. #C++初学记录(算法4)

    A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...

  9. 北大ACM试题分类+部分解题报告链接

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...

随机推荐

  1. 题目1162:I Wanna Go Home(最短路径问题进阶dijkstra算法))

    题目链接:http://ac.jobdu.com/problem.php?pid=1162 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  2. nginx+memcache实现页面缓存应用

    一.前言 nginx的memcached_module模块可以直接从memcached服务器中读取内容后输出,后续的请求不再经过应用程序处理,如php-fpm.django,大大的提升动态页面的速度. ...

  3. linux下MySQL安装及设置(二)

    MySQL二进制分发包安装 去MySql官网下MySQL classic版mysql-5.6.30-osx10.11-x86_64.tar.gz  http://dev.mysql.com/downl ...

  4. shell脚本中对简单实现对log的处理

    用shell在写小程序时,log没用像python样用logging模块可以直接使用,下面我们就简单写下用shell函数来实现log分级 #/bin/bash sys_log="/var/l ...

  5. TFS二次开发05——下载文件(DownloadFile)

    前面介绍了怎样读取TFS上目录和文件的信息,怎么建立服务器和本地的映射(Mapping). 本节介绍怎样把TFS服务器上的文件下载到本地. 下载文件可以有两种方式: using Microsoft.T ...

  6. Cglib 与 JDK动态代理的运行性能比较

    都说 Cglib 创建的动态代理的运行性能比 JDK 动态代理能高出大概 10 倍,今日抱着怀疑精神验证了一下,发现情况有所不同,遂贴出实验结果,以供参考和讨论. 代码很简单,首先,定义一个 Test ...

  7. windows远程访问ubuntu下的jupyter notebook必要配置

    0.生成配置文件(一般采用默认) jupyter notebook --generate-config 1.打开ipython, 创建一个密文密码 In [1]: from notebook.auth ...

  8. NEFU 118 - n!后面有多少个0 & NEFU 119 - 组合素数 - [n!的素因子分解]

    首先给出一个性质: n!的素因子分解中的素数p的幂为:[ n / p ] + [ n / p² ] + [ n / p³ ] + …… 举例证明: 例如我们有10!,我们要求它的素因子分解中2的幂: ...

  9. linux系统下top命令参数详解

    简介 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top显示系统当前的进程和其他状况,是一个动态显示过程,即可以通过用户按 ...

  10. hadoop第一部分-安装、测试

    一.hadoop安装(本地模式及伪分布式安装) hadoop历史版本下载网站:http://archive.apache.org/dist/运行模式:    本地模式    yarn模式 hadoop ...