hdu 1003(最大子段和)
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 204780 Accepted Submission(s): 47877
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.
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).
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.
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
14 1 4
Case 2:
7 1 6
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int N = ; int a[N];
int dp[N];
int main()
{
int tcase;
scanf("%d",&tcase);
int k =;
while(tcase--){
int n;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
int start=,endd=,t=;
int mx = dp[] = a[];
for(int i=;i<=n;i++){
if(dp[i-]>=){
dp[i] = dp[i-]+a[i];
}else{
t = i;
dp[i] = a[i];
}
if(dp[i]>mx){
mx = dp[i];
start = t;
endd = i;
}
}
printf("Case %d:\n%d %d %d\n",k++,mx,start,endd);
if(tcase!=) printf("\n");
}
return ;
}
hdu 1003(最大子段和)的更多相关文章
- HDU 1003(A - 最大子段和)
HDU 1003(A - 最大子段和) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/A 题目: ...
- HDOJ(HDU).1003 Max Sum (DP)
HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...
- dp 动态规划 hdu 1003 1087
动态规划就是寻找最优解的过程 最重要的是找到关系式 hdu 1003 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意:求最大字序列和, ...
- hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行
测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- 【ToReadList】六种姿势拿下连续子序列最大和问题,附伪代码(以HDU 1003 1231为例)(转载)
问题描述: 连续子序列最大和,其实就是求一个序列中连续的子序列中元素和最大的那个. 比如例如给定序列: { -2, 11, -4, 13, -5, -2 } 其最大连续子序列为{ 11, ...
- hdu 1003 hdu 1231 最大连续子序列【dp】
HDU1003 HDU1231 题意自明.可能是真的进步了点,记得刚开始研究这个问题时还想了好长时间,hdu 1231还手推了很长时间,今天重新写干净利落就AC了. #include<iostr ...
- HDU 1003 最大连续子段和
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)M ...
- [ACM] hdu 1003 Max Sum(最大子段和模型)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
随机推荐
- jq的each理解
1种 通过each遍历li 可以获得所有li的内容 <!-- 1种 --> <ul class="one"> <li>11a</li> ...
- 破解wingide编辑器
先到官网下载最新版的wingide(我下载的是5.1.11-1),然后安装,打开,出现下面的界面时选第三个,然后输入“ENX27-HWM6G-XYVFA-165PG”,如下图所示: 接下来你软件会给你 ...
- cuda环境下安装opencv出现nvcc warning : The 'compute_11'
警告打印: nvcc warning : The 'compute_11', 'compute_12', 'compute_13', 'sm_11', 'sm_12', and 'sm_13' arc ...
- Problem B. Harvest of Apples 莫队求组合数前缀和
Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...
- mac os x之解决npm安装包失败,或者nodejs工程缺少依赖
在国内做开发,由于各种各样的原因,导致网络总是那么不好,对于我们前端开发者,在使用npm的时候很可能因为网络问题导致包安装失败,然后我们又匆匆启动项目,导致缺少依赖等各种问题,下面将会介绍一个淘宝的n ...
- java正则 以什么开始,以什么结束
public class RegTest { public static void main(String[] args){ String regex = "\\[([\\s\\S]*?)\ ...
- maven插件理解
maven插件的主要功能是对用到的jar包进行管理,jar包先从本地仓库中获取,如果没有找到,则从远处中央仓库下载(需要联外网).本地仓库中的jar包可供所有maven工程使用,属于公共模块. mav ...
- Struts2入门(1)-第一个Struts2程序
目录结构 C:\WorkSpace\java\StrutsTest\src\main C:\WorkSpace\java\StrutsTest\src\test C:\WorkSpace\java\S ...
- PowerDesigner16 修改表或表的字段Name的时候不让Code不自动跟着变
在修改表或表的字段Name的时候不让Code不自动跟着变,设置如下: tools-> General Options-> Dialog 去掉勾选 Name To Code mirror ...
- Android中Handler导致的内存泄露
http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html Consider the follo ...