J - Max Sum

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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 //第一行代表有 t 组测试案例,问最大连续的和为多少,并输出第几位数到第几位数
//虽然,这是一道dp水题,但我从没做过,自己写出来感觉不错,62ms 比别人的慢了一倍,应该是我用了两次一重循环吧。
 #include <stdio.h>

 #define MAXN 100005

 int num[MAXN];
int dp[MAXN]; int cmp(int a,int b)
{
return a>b?a:b;
} int main()
{
int t;
int n,i,Case=;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
int max;
for (i=;i<=n;i++)
{
if (i==)
{
scanf("%d",&num[i]);
dp[i]=num[i];
max=i;
continue;
}
scanf("%d",&num[i]);
dp[i]=cmp(num[i],dp[i-]+num[i]);
if (dp[i]>dp[max]) max=i;
}
int s=max;
for (i=max;i>=;i--)
{
if (dp[i]>=)
s=i;
if (dp[i]<)
break;
}
printf("Case %d:\n",++Case);
printf("%d %d %d\n",dp[max],s,max);
if (t!=) printf("\n");
}
return ;
}
 

J - Max Sum的更多相关文章

  1. 2016huasacm暑假集训训练五 J - Max Sum

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/J 题意:求一段子的连续最大和,只要每个数都大于0 那么就会一直增加,所以只要和0 ...

  2. hdu 1003 Max Sum (DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)   ...

  3. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  4. HDU 1024 max sum plus

    A - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  5. hdu 1024 Max Sum Plus Plus

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  6. Max Sum Plus Plus——A

    A. Max Sum Plus Plus Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To ...

  7. hdu 1003 Max Sum(动态规划)

    解题思路: 本题在给定的集合中找到最大的子集合[子集合:集合的元素的总和,是所有子集合中的最大解.] 结果输出: 最大的子集合的所有元素的和,子集合在集合中的范围区间. 依次对元素相加,存到一个 su ...

  8. HDU 1003 Max Sum

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  9. Leetcode: Max Sum of Rectangle No Larger Than K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

随机推荐

  1. STL 源码分析 (SGI版本, 侯捷著)

    前言 源码之前,了无秘密 algorithm的重要性 效率的重要性 采用Cygnus C++ 2.91 for windows cygwin-b20.1-full2.exe 下载地址:http://d ...

  2. USE [EPPM] [dbo].[REFRDEL_CLEANUP]

    USE [EPPM] GO /****** Object: StoredProcedure [dbo].[REFRDEL_CLEANUP] Script Date: 2016/4/2 16:32:29 ...

  3. Sending SMS And Dialing Numbers without User Consent(Context is not needed)

    Sending SMS And Dialing Numbers without User Consent Sending SMS does not require context or user in ...

  4. azkaban2.5 具体配置以及使用方式

    azkaban支持shell.java.mapreduce以及hive的工作流调度,在对这些不同类型任务调度之前须要配置所相应的插件:azkaban总体分为两部分azkaban executor se ...

  5. 哇哦!恍然大悟般的“share”功能的实现!

    有一个问题一直困扰着我,也是我一直没有时间去了解和学习的,那就是前端(移动端)实现分享到微信.QQ好友.QQ空间.新浪微博等等平台的功能实现,虽然之前有做过,但是都是上一个领导自己写好的,我直接拿来用 ...

  6. LBS 附近的人

    1 http://www.infoq.com/cn/articles/depth-study-of-Symfony2 2 http://lbsyun.baidu.com/

  7. CMA,memory compaction,migration and fragmation

    CMA A reworked contiguous memory allocator http://lwn.net/Articles/447405/ June 14, 2011 分配大块物理上连续内存 ...

  8. Wd 西部数据

    西部数据 https://item.jd.com/3564471.html#none 打算买一个大硬盘记录代码片段.开发项目.开发工具.电影游戏等…… /** * 获取100天后的日子 * 用来做计划 ...

  9. SpringBoot使用MyBatis报错:Error invoking SqlProvider method (tk.mybatis.mapper.provider.base.BaseInsertProvider.dynamicSQL)

    © 版权声明:本文为博主原创文章,转载请注明出处  1. 错误描述 使用SpringBoot集成MyBatis框架,并且使用 mapper-spring-boot-starter 自动生成MyBati ...

  10. docker与虚拟化的联系与区别

    虚拟化技术是一个总称,是一系列实现虚拟技术的统称.从广义上来说,虚拟化技术包括了虚拟机技术和容器技术, 所谓虚拟化技术最大的特点就是将一个真实的机器进行虚拟地分割,然后分割出来的部分可以独立使用   ...