Max Sum (hdu 1003 简单DP水过)

简介:

Max Sum

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


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
 

Author
Ignatius.L
 

Recommend
We have carefully selected several similar problems for you:   1058  1203  1257  1421  1024 
 

题意:给出n个数的序列,求出最大的子串和。并输出起点和终点。

思路:dp[i]表示以i为结尾的最大子串和。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

int dp[maxn];       //dp[i]表示以i为结尾的最大子串和
int a[maxn];
int n;

int main()
{
    int i,j,t,cas=1;;
    sf(t);
    bool flag=false;
    while (t--)
    {
        if (flag) pf("\n");
        flag=true;
        sf(n);
        FRL(i,1,n+1)
            sf(a[i]);
        mem(dp,0);
        dp[1]=a[1];
        int S=1,T=1,s=1,t=1,maxx=a[1];//s,t记录当前首尾指针。S。T记录当前最大值的首尾指针
        FRL(i,2,n+1)
        {
            if (a[i]>dp[i-1]+a[i])  //假设a[i]对dp[i-1]没有贡献反而会使dp[i-1]减小,那么就以i又一次作为起点
            {
                s=i;
                t=i;
                dp[i]=a[i];
            }
            else if (a[i]<=dp[i-1]+a[i])  //a[i]比dp[i-1]+a[i]更大,就将尾指针t向后移赋为i
            {
                t=i;
                dp[i]=dp[i-1]+a[i];     //更dp[i]
            }
            if (dp[i]>maxx)   //假设当前dp[i]比之前的最大值要大,更新最大值,并记录首尾指针
            {
                S=s;
                T=t;
                maxx=dp[i];
            }
        }
        pf("Case %d:\n",cas++);
        pf("%d %d %d\n",maxx,S,T);
    }
    return 0;
}










本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5077522.html,如需转载请自行联系原作者


相关文章
|
Java 开发者 Spring
解析Spring中Bean的生命周期
解析Spring中Bean的生命周期
258 2
|
安全 数据库 C++
Python Web框架比较:Django vs Flask vs Pyramid
【10月更文挑战第6天】本文比较了Python中三个最受欢迎的Web框架:Django、Flask和Pyramid。Django功能全面,适合快速开发;Flask灵活轻量,易于上手;Pyramid介于两者之间,兼顾灵活性和可扩展性。文章分析了各框架的优缺点,帮助开发者根据项目需求和个人偏好做出合适的选择。
318 4
|
存储 Unix 数据安全/隐私保护
5款黑科技软件,走过路过,不要错过
噔噔噔噔,作为一个黑科技软件爱好者,电脑里肯定是不会缺少这方面的东西,今天的5款优质软件闪亮登场了。
292 0
|
传感器 自动驾驶 算法
本文将探讨无人驾驶汽车如何应对交通拥堵,并指出这种技术可能面临的挑战。
本文将探讨无人驾驶汽车如何应对交通拥堵,并指出这种技术可能面临的挑战。
|
安全 Java Python
Python 全局解释器锁(GIL):影响因素、机制与性能优化
Python 全局解释器锁(GIL):影响因素、机制与性能优化
944 1
|
Java jenkins 应用服务中间件
Jenkins+Gitlab+Nginx+Maven编译Java项目自动发布与基于tag版本回退(重复构建问题已解决)
Jenkins+Gitlab+Nginx+Maven编译Java项目自动发布与基于tag版本回退(重复构建问题已解决)
352 0
|
缓存 算法 Python
概率图推断之信念传播
变量消除算法有个致命的缺陷:每次查询都要要从头开始重新启动算法。这样会非常浪费资源,并且在计算上很麻烦。 这个问题也很容易避免。通过在第一次运行变量消除算法后缓存这些因子,我们可以轻松地计算新的边缘概率查询,基本上不需要额外的成本。 实现上面的功能有2中算法:信念传播(BP)和全联结树算法,本文先介绍信念传播算法。
364 0
概率图推断之信念传播
|
云安全 Kubernetes 安全
红队攻防 | 云上横向移动:利用脆弱容器实施攻击
红队攻防 | 云上横向移动:利用脆弱容器实施攻击
888 0
|
小程序
【微信小程序】全局样式、局部样式、全局配置
【微信小程序】全局样式、局部样式、全局配置
1100 0
|
存储 SQL 人工智能
《玩转 Tablestore 入门与实战》重磅来袭! 架构、原理及场景全方面解读
表格存储 Tablestore 是阿里云自研的面向海量结构化和半结构化数据的 Serverless 多模型数据存储,采用与 Google Bigtable 类似的宽表模型,天然的分布式架构,能支撑高吞吐的数据写入以及 PB 级数据存储。 表格存储 Tablestore于 2009 年阿里云成立之初即立项研发,基于底层飞天平台从零开始构建,在 2014 年正式商业化面向公有云提供服务。历经 10 年多的打磨,目前已在阿里巴巴集团、阿里云公共云以及专有云内得到广泛应用,涵盖电商、金融风控、物联网、人工智能、大数据、社交媒体等不同业务领域,支撑钉钉、优酷、手淘、IoT、计算平台等多个内部核心 BU
17531 0
《玩转 Tablestore 入门与实战》重磅来袭! 架构、原理及场景全方面解读

热门文章

最新文章