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. sencha touch 2.2.1 自定义彩色图标按钮(button+ico)

    sencha touch 2.2.1 这个版本使用了新的按钮模式,不过他只提供了少部分的按钮样式.我们可以加一些自定义的ico样式 新加ico样式lower .x-button .x-button-i ...

  2. linux ntp时间服务器配置

    Network Time Protocol (NTP) 也是RHCE新增的考试要求. 学习的时候也顺便复习了一下如何设置Linux的时间,现在拿出来和大家分享 设置NTP服务器不难但是NTP本身是一个 ...

  3. vue.js - 解决vue-cli打包后自动压缩代码

    一.webpack中引入的压缩代码 /build/webpack.prod.conf.js const OptimizeCSSPlugin = require('optimize-css-assets ...

  4. 使用jetty的continuations实现"服务器推"

    在实际的开发中,我们可能会有这样的场景:许多客户端都连接到服务器端,当有某个客户端的消息的时候,服务器端会主动"推"消息给客户端,手机app的推送是一个典型的场景(IOS的推送都是 ...

  5. kvm虚拟机不能使用virsh shutdownw命令关闭虚拟机的解决方法

    今天笔者在对kvm虚拟机进行管理时,使用virsh shutdown命令关闭指定的虚拟机时,发现虽然有如下的提示,但其实虚拟机却一直不会真正的关闭. 经过查看virsh命令帮助和上网查询,才得知vir ...

  6. wpgcms---banner图怎么调用

    使用wpgcms调用banner图,首先新建应用为 自定义应用,然后添加对应的字段信息,例如: 具体调用方式: <ul> {% set bannerlist = wpg.appdata.g ...

  7. linux 系统信息查看

    查看系统版本:lsb_release -a 查看内核版本:uname -a 查看cpu型号:cat /proc/cpuinfo 查看硬盘空间情况df -lm 查看内存:free -m  VGA显卡:l ...

  8. url写法细节

  9. Django-MySQL数据库使用01

    Django连接数据库的要求:1)Pycharm运行Django平台:2)MySQL数据库.本文的前提是这两个平台读者都已经都正确安装,未安装的朋友请自行百度.说明一下我用的Django是2.1版本, ...

  10. iOS-多语言版本的开发(一)(转载)

    引言  多语言 & 本地化,随你怎么叫,道理差不多:一个App 要想走出国门,只支持一种语言是不能够的,也是不可能的,多元化世界已经融入我们的生活,对于一些应用,开发多语言版本,已经是不可避免 ...