A - 最大子段和

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
 
 
题解:

求最大子序列和,及其初始和结束位置。  如果有多解,输出最先算出的。
两种方法,一看就懂得了
 
#include <cstdio>
int main()
{
int T,k, R, L, Max, sum, t, total=, n;
scanf("%d", &T);
while(T--)
{
sum = Max =-; //足够小就好
scanf("%d", &n);
for(int i=; i<=n; i++)
{
scanf("%d", &t);
if (sum<)
{
sum = t, k = i;
}
else
sum += t;
if (Max < sum) Max = sum, L = k, R = i; //L R分别为初始和结束位置
}
printf("Case %d:\n", total++);
printf("%d %d %d\n", Max, L, R );
if (T) printf("\n");
}
return ;
}
#include<iostream>
using namespace std;
int a[],b[];
int main()
{
int n,T,s,t,max,total,k=;
cin>>T;
while(T--)
{
cin>>n;
for(int i=; i<=n; i++)
cin>>a[i];
b[]=a[];
for(int i=; i<=n; i++)
{
if(b[i-]<)
b[i]=a[i];
else
b[i]=b[i-]+a[i];
}
max=b[];
s=;
for(int i=; i<=n; i++)
{
if(b[i]>max)
{
max=b[i];
s=i;
}
}
t=s;
total=;
for(int i=s; i>=; i--)
{
total+=a[i];
if(total==max) t=i;
}
cout<<"Case "<<k++<<":"<<endl;
cout<<max<<" "<<t<<" "<<s<<endl;
if(T) cout<<endl;
}
}

hdu1003 Max Sum(经典dp )的更多相关文章

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

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

  2. 杭电60题--part 1 HDU1003 Max Sum(DP 动态规划)

    最近想学DP,锻炼思维,记录一下自己踩到的坑,来写一波详细的结题报告,持续更新. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Problem ...

  3. ACM学习历程—HDU1003 Max Sum(dp && 最大子序列和)

    Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...

  4. 解题报告:hdu1003 Max Sum - 最大连续区间和 - 计算开头和结尾

    2017-09-06 21:32:22 writer:pprp 可以作为一个模板 /* @theme: hdu1003 Max Sum @writer:pprp @end:21:26 @declare ...

  5. Max Sum (dp)

    Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. F ...

  6. 最大子序列和——HDU-1003 Max Sum

    题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义dp[i]表示以a[i]为结尾的子序列的和的最大值,因而最大 ...

  7. hdu 1003 Max sum(简单DP)

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

  8. HDU-1003 Max Sum(动态规划,最长字段和问题)

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

  9. HDU1003 Max Sum(求最大字段和)

    事实上这连续发表的三篇是一模一样的思路,我就厚颜无耻的再发一篇吧! 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 -------------- ...

随机推荐

  1. 【转】AngularJS 日期格式化 字典

    本地化日期格式化: ({{time|date:'medium' }})Apr 14, 2016 4:40:08 PM ({{time | date:'short' }})4/14/16 4:40 PM ...

  2. EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right outer join)、多表查询

    场景:在实际的项目中使用EntityFramework都会遇到使用Ef处理连接查询的问题,这里做一些小例子如何通过Linq语法处理内连接(inner join).外连接(left/right oute ...

  3. Struts2方法调用的三种方式

    在Struts2中方法调用概括起来主要有三种形式 第一种方式:指定method属性 <action name="student" class="com.itmyho ...

  4. .net自定义控件

    一.[.net自定义控件]TextBox控件重写 之NumTextBox 参考博客:http://www.yongfa365.com/Item/NumTextBox.html 二.[.net自定义控件 ...

  5. 工程建立多个source folder

    在工程中,想在建立多个source filder,总是报错,报错信息是 Cannot nest 'GzEdu/src/c' inside 'GzEdu/src'. To enable the nest ...

  6. CreateThread函数

    当使用CreateProcess调用时,系统将创建一个进程和一个主线程. CreateThread将在主线程的基础上创建一个新线程,大致做例如以下步骤: 1在内核对象中分配一个线程标识/句柄,可供管理 ...

  7. Android 图片选择器

    图片选择器,遍历系统所有图片并显示,点击查看大图,长按选中,并将结果返回 字体颜色res/color建立text_selecor.xml <selector xmlns:android=&quo ...

  8. Project Euler:Problem 55 Lychrel numbers

    If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...

  9. [Angular 2] Custom Validtors

    Create a custom validtor which only accepts the string start with '123'; function skuValidator(contr ...

  10. nodejs这个过程POST求

    下面是一个web登陆模拟过程.当我们问一个链接,你得到一个表格,然后填写相应的表格值,然后提交登陆. var http = require('http'); var querystring = req ...