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. Solidworks草图或者特征无法删除怎么办

      单击重新建模之后即可删除.

  2. const 使用方法具体解释

    const使用方法具体解释 面向对象是C++的重要特性.  可是c++在c的基础上新添加的几点优化也是非常耀眼的 就const直接能够代替c中的#define 下面几点非常重要,学不好后果也也非常严重 ...

  3. linger博客原创性博文导航

    linger博客原创性博文导航 http://blog.csdn.net/lingerlanlan 大学研究游戏外挂技术開始了此博客.断断续续写了些博文. 后来,開始机器学习和深度学习的研究工作,因为 ...

  4. Python_Select解析

    selcet(等待I/O完成)的介绍: select同时监控多个socket,select()的机制提供了fd_set的数据结构,实际是long类型的数组,优点是跨平台性,select的缺点在于单个进 ...

  5. linux驱动current,引用当前进程,及task_struct(转)

    尽管内核模块不象应用程序一样顺序执行, 内核做的大部分动作是代表一个特定进程的. 内核代码可以引用当前进程, 通过存取全局项 current, 它在 <asm/current.h> 中定义 ...

  6. xUtils介绍 -- DbUtils、ViewUtils、HttpUtils、BitmapUtils

    转载注明出处:https://github.com/wyouflf/xUtils xUtils简单介绍 xUtils 包括了非常多有用的android工具. xUtils 支持大文件上传,更全面的ht ...

  7. STL学习笔记(变动性算法)

    本节描述的算法会变动区间内的元素内容.有两种方法可以变动元素内容: 1.运用迭代器遍历序列的过程中,直接加以变动 2.将元素从源区间赋值到目标区间的过程中加以变动 复制(copy)元素 OutputI ...

  8. 【VBA】VBA编写的,将一列中相同的内容的行提取出来单独生成文件

    数据如上图所示,点击RUN后的运行结果如下: 得到该文件夹,文件夹内容如上图. 代码如下: Private Sub Command_OLIVER() Dim arr arr = Range(" ...

  9. IP数据库生成器

    代码地址如下:http://www.demodashi.com/demo/12688.html 项目放在github上,python版本ipdb_creator,java版本ip-locator. 项 ...

  10. BufferedReader 使用 readLine() 读取 UTF-8 格式的文本第一行第一个字符是空字符的解决办法

    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(ksmgVo.getFiledata( ...