链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1003

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

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

5 6 -1 5 4 -7
6 5 10 14 7  
Case 1:
14 1 4

7 0 6 -1 1 -6 7 -5
0 6 5 6 0 7 2
Case 2:
7 1 6

 

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue> using namespace std; #define N 110000
#define INF 0xffffff int sum[N]; int main()
{ int t, iCase=;
scanf("%d", &t); while(t--)
{
int i, n, Max=-INF, Min=INF, a, L, Left, Right; scanf("%d", &n); for(i=; i<=n; i++)
{
scanf("%d", &a);
sum[i] = sum[i-]+a;
}
/// Left = Right = 1;
for(i=; i<=n; i++)
{
///找出最小的sum[i]
if(sum[i-]<Min)
{
Min = sum[i-];
L = i;
}
///找出最大的Max
if(sum[i]-Min>Max)
{
Max = sum[i]-Min;
Left = L;
Right = i;
}
} printf("Case %d:\n", iCase++);
printf("%d %d %d\n", Max, Left, Right); if(t) printf("\n");
}
return ;
}

Max Sum -- hdu -- 1003的更多相关文章

  1. HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)

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

  2. HDU OJ Max sum 题目1003

     #include <iostream> #include<stdio.h> #include<stdlib.h> using namespace std; i ...

  3. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  4. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  5. HDU 1003 Max Sum【动态规划求最大子序列和详解 】

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

  6. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  7. hdu 1003 Max Sum (DP)

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

  8. HDU 1003 Max Sum (动规)

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

  9. hdu 1003 Max sum(简单DP)

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

随机推荐

  1. PHP 设计模式 原型模式(Prototype)之深/浅拷贝

      看PHP 设计模式 原型模式(Prototype)时,衍生出一个扩展问题之 原型拷贝的浅拷贝和深拷贝问题(不管写Java还是写PHP还是写JS时都多多少少遇到过对象拷贝问题)   比如写前端页面时 ...

  2. 转--NLTK的内置函数

    NLTK的内置函数 1. 词语索引 (1) concordance函数    给出一个指定单词每一次出现,连同上下文一起显示. >>>text1.concordance('monst ...

  3. Hibernate SQL 查询

    本文转载自:https://www.cnblogs.com/li3807/p/6358386.html Hibernate 支持使用原生的SQL查询,使用原生SQL查询可以利用某些数据库特性,原生SQ ...

  4. ESN

    1.对于一般的硬件设备,ESN是设备序列号,主要用来识别设备,包括未来服务鉴权的需要 2.对于需要license的设备,ESN也是设备序列号的意思,只不过这个序列号可能是根据设备硬件信息算出来的一串字 ...

  5. echarts地图扩展文件使用geoJson格式。

    echarts地图扩展文件使用geoJson格式. 1.在线生成 http://ecomfe.github.io/echarts-map-tool/  这里可以生成省市区的json,但是最多生成到”区 ...

  6. ThreadCachedInt

    folly/ThreadCachedInt.h High-performance atomic increment using thread caching. folly/ThreadCachedIn ...

  7. AngularJS学习笔记(1)——MVC模式的清单列表效果

    MVC模式的清单列表效果 使用WebStorm新建todo.html并链入bootstrap.css.bootstrap-theme.css.angular.js.要链入的相关css和js文件预先准备 ...

  8. 读取Excel里面的内容转为DataTable

    using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using Sy ...

  9. 从LinqPad入门Linq

    为什么要学习Linq? 在我们日常开发中,经常需要从数据库中执行各式各样的查询来获取需要的数据.但是如果需要对查询获得的数据进行二次筛选呢?linq就提供了对于可枚举类型(实现IEnumerable接 ...

  10. “MSDTC 事务的导入失败: Result Code = 0x8004d00e。

    今天搞数据库移植,也把分布式程序移植了一下,一直报上面的错误,差点要重装了  %>_<%,网上看了几篇文章偶然间用DPCPING工具测试了下连接,发现不行.想到host文件的配置,发现其中 ...