Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 324393    Accepted Submission(s): 77146

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
 
 

【题意】

  给定一个序列a[1],a[2],a[3],.....,a[n],来计算子序列的最大和。例如,给定序列(6,-1,5,4,-7),这个序列中的最大和是 6 +(-1)+ 5 + 4 = 14。

  输入的第一行包含一个整数T(1<=T<=20),它表示测试用例的数量。然后是T行,每一行以N开头(1<=N<=100000),然后是N个整数(所有整数都在-1000到1000之间)

  对于每个测试用例,输出两行。第一行是“Case #:”,#表示测试用例的数量。第二行包含三个整数,序列中的最大和,子序列的起始位置,子序列的结束位置。如果有多个结果,输出第一个。在两种情况之间输出空行。

【代码】

#include <stdio.h>
#include <stdlib.h>
int main(){
int i,j,n,t;
scanf("%d",&t);
for(i=;i<=t;i++){
int *a,first=,last=,sum=,tmp=,max=-;
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));
for(j=;j<n;j++){
scanf("%d",&a[j]);
sum += a[j];
if(sum>max){
max=sum;
first=tmp;
last=j+;
}
if(sum<){
sum=;
tmp=j+;
}
}
printf("Case %d:\n%d %d %d\n",i,max,first,last);
if(i!=t)printf("\n");
a=NULL;
}
return ;
}
 
 
 
 
 
 
 
 

HDU1003 最大连续子序列的更多相关文章

  1. hdu1003 Max Sum【最大连续子序列之和】

    题目链接:https://vjudge.net/problem/HDU-1003 题目大意:给出一段序列,求出最大连续子序列之和,以及给出这段子序列的起点和终点. 解题思路:最长连续子序列之和问题其实 ...

  2. dp经典问题-最大连续子序列和 hdu1003

    题目描述: 这道题我先后做过三遍,结果每一遍都没有做出来.今天再仔仔细细的研究了一下,才发现用动态规划更好理解. 关于求最大连续子序列和的博文转载如下:https://www.cnblogs.com/ ...

  3. HDU 1231:最大连续子序列(DP)

    pid=1231">最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  4. lintcode 最长上升连续子序列 II(二维最长上升连续序列)

    题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/ 最长上升连续子序列 I ...

  5. 最大连续子序列乘积(DP)

    题目来源:小米手机2013年校园招聘笔试题 题目描述: 给定一个浮点数序列(可能有正数.0和负数),求出一个最大的连续子序列乘积. 输入: 输入可能包含多个测试样例.每个测试样例的第一行仅包含正整数 ...

  6. DP专题训练之HDU 1231 最大连续子序列

    Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j < ...

  7. HDU 1231 最大连续子序列(水题)

    题目链接: 传送门 最大连续子序列 Time Limit: 1000MS     Memory Limit: 32768 K Description 给定K个整数的序列{ N1, N2, ..., N ...

  8. HDU-1231 简单dp,连续子序列最大和,水

    1.HDU-1231 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=1231 3.总结:水 题意:连续子序列最大和 #include<iostre ...

  9. HDU 1231:最大连续子序列 解题报告

    第一次写博客, 自己总结写出了一道题感觉值得保存. 自己总结的规律:求最大连续子序列, 可以先求包括第N项在内的前N项最大值, (因为每一项都求过后, 前N项最大值最大的那一个元素所连续的序列即为最大 ...

随机推荐

  1. Idea中提交SVN或git时,忽略某些文件不提交

    第一步:点击 setting 第二步:点击Editor下的File Types 第三步:编辑,在后面添加 *.iml;*.idea;*.gitignore;*.sh;*.classpath;*.pro ...

  2. hibernate配置和映射文件

    映射文件 <?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping ...

  3. 2019HDU多校第五场A fraction —— 辗转相除法|类欧几里得

    题目 设 $ab^{-1} = x(mod \ p)$,给出 $x,p$,要求最小的 $b$,其中 $0< a < b, \ 1 < x<p,\ 3 \leq x\leq {1 ...

  4. Python操作MySQL数据库,插入重复数据

    sql = "INSERT  INTO test_c(id,name,sex)values(%s,%s,%s)" param = (1,'AJ','MAN') n = cursor ...

  5. [Luogu] 金字塔神话

    这是出给pj的题ccccc #include <bits/stdc++.h> #define ll long long #define INF 2147483647 #define y1 ...

  6. Codevs 2492 上帝造题的七分钟 2(线段树)

    时间限制: 1 s 空间限制: 64000 KB 题目等级 : 大师 Master 题目描述 Description XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. " ...

  7. 解决ScrollView中Recyclerview显示不全,滑动不流畅的问题

    这个问题经常会碰到,看了下网上关于这个问题的解决方案,有很多都是复制粘贴的,并不能根本解决问题 比较有效的一种方案是在Recyclerview的外层套一个RelativeLayout 然后设置recy ...

  8. elasticsearch _create api创建一个不存在的文档

    https://www.elastic.co/guide/cn/elasticsearch/guide/current/create-doc.html当我们索引一个文档, 怎么确认我们正在创建一个完全 ...

  9. 【面试题总结】1、统计字符串中某个单词出现的次数(1-C++实现)

    [解决方法一]C++ map解决 一.map中的find函数: 用于查找map中是否包含某个关键字条目,传入的参数是要查找的key,最后返回一个迭代器,如果没有找到,则返回的迭代器等于end()返回的 ...

  10. CTF辅助脚本

    首先推荐这篇文章,网上有多次转载,这是我见过日期比较早的 CTF中那些脑洞大开的编码和加密 凯撒密码 flag='flag{abcdef}' c='' n=20 for i in flag: if ' ...