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. Shell语法整理,持续维护

    Shell shell条件表达式: CONDITIONAL EXPRESSIONSConditional expressions are used by the [[ compound command ...

  2. oAuth 认证和授权原理

    什么是OAuth授权?   一.什么是OAuth协议 OAuth(开放授权)是一个开放标准. 允许第三方网站在用户授权的前提下访问在用户在服务商那里存储的各种信息. 而这种授权无需将用户提供用户名和密 ...

  3. netstat命令的安装

    yum -y install net-tools    (可以生成ifconfig命令,netstat命令)

  4. JPEG图片扩展信息读取与修改

    extends:http://www.2cto.com/kf/201405/303813.html 读写均是键值对的方式,需要注意的是值的类型需要严格按照api定义格式. 支持读写节点为: 1.TAG ...

  5. dhroid - Perference

    SharedPreferences 是我们开发android使用很多的工具通常我们是这样使用的 SharedPreferences share=getSharedPreferences("n ...

  6. 7.24 IO多路复用和协程代码笔记

    1. 复习 # !/usr/bin/env python # !--*--coding:utf-8 --*-- # !@Time :2018/7/23 11:49 # !@Author TrueNew ...

  7. Save vtkMatrix4x4 to a file 保存到文件

    vtkMatrix4x4是VTK中的一个表示4x4矩阵的一种数据结构,有时候我们想把其保存到一个文件中,那么可以使用如下的代码: void writeVtkMatrix4x4ToFile(const ...

  8. inline-blcok 之间的空白间隙

    前言: inline-blcok 布局时,通常情况下, inline-blocks 之间有空白,尽管通常我们是不想要的,毕竟不像padding或者margin一样好控制,如图: <div cla ...

  9. rs.getMetadata

    元数据(MetaData),即定义数据的数据.打个比方,就好像我们要想搜索一首歌(歌本身是数据),而我们可以通过歌名,作者,专辑等信息来搜索,那么这些歌名,作者,专辑等等就是这首歌的元数据.因此数据库 ...

  10. HDU - 5969 最大的位或 想法题

    http://acm.hdu.edu.cn/showproblem.php?pid=5969 (合肥)区域赛签到题...orz 题意:给你l,r,求x|y的max,x,y满足l<=x<=y ...