Problem A
Problem 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
题目:给你一个序列,求最大连续序列的和;
解题思路:这个题上学期就写过了,Max
Sum,上学期最后期末复习,不爱学高数了,就把杭电的题挨着写,第一页写了一半;但是没用DP,用一个maxn记录当前序列最大的和,从头枚举连续数列,最后输出maxn;
感悟:总结隔着一天才写的,因为时间太长了,早忘了当初写的什么;
代码:
#include
int main()
{ int
t,i,max=-1001,start=0,end=0,temp=0,sum=0;
int a;
long n;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
if(i!=1)
printf("\n");
scanf("%d",&n);
max=-1001,start=0,end=0,temp=1,sum=0;
for(int j=1;j<=n;j++)
{
scanf("%d",&a);
sum+=a;
if(sum>max)
{
max=sum;
end=j;
start=temp;
}
if(sum<0)
{
sum=0;
temp=j+1;
}
}
printf("Case %d:\n",i);
printf("%d %d %d\n",max,start,end);
}
return 0;
}
Problem A的更多相关文章
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [LeetCode] Water and Jug Problem 水罐问题
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案
$s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...
随机推荐
- HDFS源码分析之NameNode(2)————Format
在Hadoop的HDFS部署好了之后并不能马上使用,而是先要对配置的文件系统进行格式化.在这里要注意两个概念,一个是文件系统,此时的文件系统在物理上还不存在,或许是网络磁盘来描述会更加合适:二就是格式 ...
- [bzoj1059] [ZJOI2007] 矩阵游戏 (二分图匹配)
小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏--矩阵游戏.矩阵游戏在一个N *N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两种操作:行交换操作:选 ...
- windows phone 模拟器
window phone 模拟器启动报错 修改Bios设置,我的是yoga pro 2,只修改 即可.启动成功
- Nunit测试工具使用实例
前言: 本文主要是介绍了Nunit的基本使用,其中参详了很多已有的文章,由于最近要使用其进行测试,所以对网上的文章做了下整理,同时加入了一些自己的实践. NUnit的属性 TestFixture 它标 ...
- webpack2使用ch3-自动化生成.html和内部引入的js自动更改
1 安装webpack插件 $ cnpm install html-webpack-plugin --save-dev 安装成功后 2 由于之前 dist/目录下,编译后的.html和.js会混在一起 ...
- 【实验吧】Once More
<?php if (isset ($_GET['password'])) { if (ereg ("^[a-zA-Z0-9]+$", $_GET['password']) = ...
- Struts2 02--通配符
在以前没有使用Struts时,web与前台的数据交互通过Servlet+jsp页面.一个增删改查往往需要写四个Servlet来处理数据:在使用struts之后,Servlet不再被使用,而是通过 ...
- 胡小兔的OI日志3 完结版
胡小兔的 OI 日志 3 (2017.9.1 ~ 2017.10.11) 标签: 日记 查看最新 2017-09-02 51nod 1378 夹克老爷的愤怒 | 树形DP 夹克老爷逢三抽一之后,由于采 ...
- cocos2dx - 伤害实现
接上一节内容:cocos2dx - 生成怪物及AI 本节主要讲如何通过创建简单的矩形区域来造成伤害 在小游戏中简单的碰撞需求应用box2d等引擎会显得过于臃肿复杂,且功能不是根据需求定制,还要封装,为 ...
- Go语言备忘录:net/http包的使用模式和源码解析
本文是晚辈对net/http包的一点浅显的理解,文中如有错误的地方请前辈们指出,以免误导! 转摘本文也请注明出处:Go语言备忘录:net/http包的使用模式和源码解析,多谢! 目录: 一.http ...