Max Sum

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

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
 
解题心得:
  这个题是一个简单地动态规划题,题意:输入n个数,求它的最大的子序列和。首先写出状态转移方程: sum[i]=max{sum[i-1]+a[i],a[i]}。
  s数组是记录开始位置,每当sum加上一个a[i],值小于0的时候,s取用新的值。ans是记录结束位置,每当sum加上一个a[i],值大于等于0的时候,更新一下。
    我又在格式问题上出错了,以后要更加注意格式问题,最后一行不需要换行!
  也可以参考这个人的思路,http://blog.csdn.net/code_pang/article/details/7772200,通过枚举发现的规律。
 
最后是代码:
#include <iostream>
#include <cstdio> using namespace std; int main()
{
int t;
int n;
int a[];//存储序列
int sum[];//存储以每个数为结尾的子序列和
int s[];//存储开始位置
int ans;//结束位置
scanf("%d",&t);
for(int i=;i<=t;i++){
scanf("%d",&n);
for(int i1=;i1<n;i1++){
scanf("%d",&a[i1]);
}
ans=;
sum[]=a[];
s[]=;
for(int j=;j<n;j++){
if(sum[j-]>=){
sum[j]=sum[j-]+a[j];
s[j]=s[j-];
}else{
sum[j]=a[j];
s[j]=j;
}
if(sum[ans]<sum[j])
ans=j;
}
if(i<t){
printf("Case %d:\n%d %d %d\n",i,sum[ans],s[ans]+,ans+);
printf("\n");
}else{
printf("Case %d:\n%d %d %d\n",i,sum[ans],s[ans]+,ans+);
}
}
return ;
}
  

HDU 1003 Max Sum的更多相关文章

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

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

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

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

  3. hdu 1003 Max Sum (DP)

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

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

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

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

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

  6. HDU 1003 Max Sum (动规)

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

  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 解题报告

    题目大意:求一串数字中,几个连续数字加起来最大值,并确定起始和最末的位置. 思路:这是一题DP题,但是可以用尺取法来做.我一开始不会,也是看了某大神的代码,然后有人告诉我这是尺取法,现在会了. //尺 ...

  9. HDU 1003 Max Sum(AC代码)

    #include <stdio.h> int main(){ int i,t,j,n,x; int start,end,temp,max,sum; scanf("%d" ...

随机推荐

  1. 【HDU 5363】Key Set

    题 Description soda has a set $S$ with $n$ integers $\{1, 2, \dots, n\}$. A set is called key set if ...

  2. 【BZOJ-3293&1465&1045】分金币&糖果传递×2 中位数 + 乱搞

    3293: [Cqoi2011]分金币 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 854  Solved: 476[Submit][Status] ...

  3. [NOIP2010] 提高组 洛谷P1541 乌龟棋

    题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起 ...

  4. POJ 1469 COURSES

    COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20478   Accepted: 8056 Descript ...

  5. 如何在 Arch Linux 的终端里设定 WiFi 网络

    如果你使用的是其他 Linux 发行版 而不是 Arch CLI,那么可能会不习惯在终端里设置 WiFi.尽管整个过程有点简单,不过我还是要讲一下.在这篇文章里,我将带领新手们通过一步步的设置向导,把 ...

  6. MyEclipse------如何连接MySQL

    testconnection.jsp <%@ page language="java" import="java.util.*" pageEncoding ...

  7. 全程图解 手把手教您开启windows终端服务

    一.什么是远程桌面? 远程桌面是微软公司为了方便网络管理员管理维护服务器而推出的一项服务.从windows 2000 server版本开始引入,网络管理员使用远程桌面连接程序连接到网络任意一台开启了远 ...

  8. Unable to open liblaunch_sim.dylib. Try reinstalling Xcode or the simulator

    关于Xcode7 Beta报错 simulator runtime is not available. Unable to open liblaunch_sim.dylib Try reinstall ...

  9. linux 多线程 LinuxThreads(转)

    Linux 线程实现机制分析 http://www.ibm.com/developerworks/cn/linux/kernel/l-thread/ 上面文章分析的非常透彻 按照教科书上的定义,进程是 ...

  10. 机器学习公开课笔记(4):神经网络(Neural Network)——表示

    动机(Motivation) 对于非线性分类问题,如果用多元线性回归进行分类,需要构造许多高次项,导致特征特多学习参数过多,从而复杂度太高. 神经网络(Neural Network) 一个简单的神经网 ...