Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 181284    Accepted Submission(s): 42384

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

/*找出一个数组中和最大的一串数字,求和并输出起点和终点,简单的dp,一个数字一个数字开始加
找到小于0的话就重新开始找,否则就继续加,开始时想用01背包做,但是发现全部的数字加起来可能是
负数,这样的话01背包就没办法列出所有的情况,并且起点和终点也不好记录*/
#include<stdio.h>
#include<string.h>
int a[100010];
int main()
{
int max,b,e,t,n,sum;
int Case=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int k=1;
int max=0,flog=0;
sum=b=e=0;
for(int j=1;j<=n;j++)
{
if(a[j]>0)
flog=1;//如果数组中没有一个数大于0,那么就找出最大的负数
sum+=a[j];
if(sum<0)
{
sum=0;
k=j+1;//如果sum<0,就让sum=0重新开始加
}
if(sum>max)
{
max=sum;//找到比max大的和就开始替换
b=k;
e=j;
}
}
if(flog==0)
{
max=a[1];
b=1,e=1;
for(int j=2;j<=n;j++)
if(max<a[j])
{
max=a[j];
b=j;e=j;
}
}
printf("Case %d:\n",Case++);
printf("%d %d %d\n",max,b,e);
if(t>=1)
printf("\n");
}
return 0;
}
 

Max Sum--hdoj 1003 dp的更多相关文章

  1. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

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

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

  3. hdu 1003 Max Sum(基础dp)

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

  4. Max Sum -- hdu -- 1003

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

  5. hdu 1024 Max Sum Plus Plus DP

    Max Sum Plus Plus Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php ...

  6. Max Sum Plus Plus-HDU1024(dp)

    Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...

  7. hdu1003 Max Sum(经典dp )

      A - 最大子段和 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descr ...

  8. HDU OJ Max sum 题目1003

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

  9. HDU1024 Max Sum Plus Plus —— DP + 滚动数组

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS ...

  10. hdu Max Sum Plus Plus(dp+滚动数组)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 m为段,要深刻理解题意,并没有说是段与段要连接. 题解链接:http://blog.csdn.n ...

随机推荐

  1. scrollWidth clientWidth offsetWidth

    scrollWidth:对象的实际内容的宽度,不包边线宽度,会随对象中内容超过可视区后而变大.         实际内容+padding                 不包括滚动条 边框client ...

  2. Java中last_insert_id的使用

    last_insert_id的作用是:在当前表中的主键是自增时,插入一条新记录的同时使用last_insert_id可以获取当前的新记录的主键id. 下面是一个例子: import java.sql. ...

  3. poj1328 Radar Installation 区间贪心

    题目大意: 在X轴选择尽量少的点作为圆心,作半径为d的圆.使得这些圆能覆盖所有的点. 思路: 把每个点都转化到X轴上.也就是可以覆盖这个点的圆心的位置的范围[a,b].然后按照每个点对应的a从小到大排 ...

  4. RecyclerView 通用 Adapter

    先看使用方法

  5. node 连接MySQL及其分装, 连接池连接

    const mysql = require('mysql') const config = require('./../../config/config.default') var connectio ...

  6. Java_Web之分层架构

    当我们把业务处理的代码与JSP代码混在一起,不易于阅读,不易于代码维护,这就需要分层. 分层模式 1.分层模式是最常见的一种架构模式 2.分层模式是很多架构模式的基础 分层 将解决方案的组件分隔到不同 ...

  7. jQuery顺序加载图片(终版)

    这一篇是对上一篇(jQuery顺序加载图片(初版)--http://www.cnblogs.com/newbie-cc/p/3707504.html)的改进. function loadImage(i ...

  8. beetl模板入门例子

    加入maven依赖 <dependency> <groupId>org.beetl</groupId> <artifactId>beetl-core&l ...

  9. extension Array where Element 代码学习

    var fieldNames: [String] { let p = UnsafePointer<Int32>(self.pointer) return Array(utf8Strings ...

  10. react-native 横向滚动的商品展示

    在app中会有这种页面 像这样商品是横向的,而且要滚动,思路是利用 ScrollView 横向的滚动 思路: (a): 横向滚动的整体作为一个组件  ShopCenter {/*** 横向滚动 *** ...