http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1049

令 dp[i]表示为以a[i]结尾的最大子段和,则  dp[i]=max(dp[i-1]+a[i],a[i]);

包含a[i-1] : dp[i]=dp[i-1]+a[i];

不包含a[i-1] : dp[i]=a[i];

然后扫一遍dp[i]求出最大值。 时间复杂度O(n),空间复杂度 O(n);

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 50010
#define mod 1000000000
using namespace std; ll dp[N];
ll f[N]; int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%lld",&f[i]);
dp[]=f[];
for(int i=;i<n;i++)
dp[i]=max(dp[i-]+f[i],f[i]);
ll m=;
for(int i=;i<n;i++)
if(dp[i]>m) m=dp[i];
printf("%lld\n",m);
return ;
}

可以发现其实 dp数组是不用保存的,我只要每次都更新一个最大值即可。空间复杂度可以优化成 O(1) .

#include<cstdio>
#include<algorithm>
using namespace std; int main()
{
int n;
long long a,ans=,m=;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%lld",&a);
ans=max(ans+a,a);
m=max(m,ans);
}
printf("%d\n",m);
return ;
}

更快速的方法是不断读取 数组元素 a,然后sum累加a,更新最大值,如果小于0,就置为0.这样省去调用库函数的时间。

 #include<stdio.h>
int main()
{
__int64 sum,sum1,tem;
sum=sum1=;
__int64 t,i;
scanf("%I64d",&t);
for(i=;i<t;i++)
{
scanf("%I64d",&tem);
sum1=sum1+tem;
if(sum1<)
sum1=;
if(sum1>sum)
sum=sum1; }
printf("%I64d\n",sum);
}

保存最大字段和的起始和终止位置。

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 50010
#define mod 1000000000
using namespace std; int main()
{
int n;
long long a,ans=,m=;
int begin,left,right;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%lld",&a);
if(ans>=)
{
ans+=a;
}
else
{
ans=a;
begin=i;
}
if(ans>m)
{
left=begin;
right=i;
m=ans;
}
}
printf("%d %d %lld\n",left,right,m);
return ;
}

51nod 1049 1049 最大子段和 (dp)的更多相关文章

  1. 51nod 1065 最小正子段和

    题目链接:51nod 1065 最小正子段和 房教说用前缀和做,然后看了别人博客懂了后就感觉,这个真有意思... #include<cstdio> #include<cstring& ...

  2. 51nod 1293 球与切换器 | DP

    51nod 1293 球与切换器 | DP 题面 有N行M列的正方形盒子.每个盒子有三种状态0, -1, +1.球从盒子上边或左边进入盒子,从下边或右边离开盒子.规则: 如果盒子的模式是-1,则进入它 ...

  3. 51Nod 1049:最大子段和(dp)

    1049 最大子段和  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 N个整数组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+ ...

  4. 51nod 1051 最大子矩阵和 【最大子段和DP变形/降维】

    [题目]: 一个M*N的矩阵,找到此矩阵的一个子矩阵,并且这个子矩阵的元素的和是最大的,输出这个最大的值. 例如:*3的矩阵: - - - - 和最大的子矩阵是: - - Input 第1行:M和N, ...

  5. 51Nod 1050 循环数组最大子段和 | DP

    Input示例 6 -2 11 -4 13 -5 -2 Output示例 20 分析: 有两种可能,第一种为正常从[1 - n]序列中的最大子字段和:第二种为数组的total_sum - ([1-n] ...

  6. bzoj 1049: [HAOI2006]数字序列【dp+二分+瞎搞】

    第一问明显就是用b[i]=a[i]-i来做最长不下降子序列 然后第二问,对于一对f[i]=f[j]+1的(i,j),中间的数一定要改的,并且是等于b[i]或者b[j],我不会证,然后因为是随机数据,所 ...

  7. 51nod 循环数组最大子段和

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1050 对于普通的数组,只要求一次最大子段和即可.但是这题是可以循环的,所 ...

  8. 51nod 1051 最大子矩阵和(dp)

    题目链接:51nod 1051 最大子矩阵和 实质是把最大子段和扩展到二维.读题注意m,n... #include<cstdio> #include<cstring> #inc ...

  9. 51nod 1412 AVL树的种类(dp)

    题目链接:51nod 1412 AVL树的种类 开始做的时候把深度开得过小了结果一直WA,是我天真了.. #include<cstdio> #include<cstring> ...

随机推荐

  1. 【bzoj1011】[HNOI2008]遥远的行星

    1011: [HNOI2008]遥远的行星 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 3711  Solved ...

  2. angularJs 问题

    1. IE不能渲染指令中的 style="background-color",而chrome和firefox可以 <!DOCTYPE html> <html ng ...

  3. aspx文件、aspx.cs文件、aspx.designer.cs文件之讲解

    .aspx文件:(页面)书写页面代码.存储的是页面design代码.只是放各个控件的代码,处理代码一般放在.cs文件中. .aspx.cs文件:(代码隐藏页)书写类代码.存储的是程序代码.一般存放与数 ...

  4. 使用eclipse maven遇到的错误(转)

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:2.5:resources (defaul ...

  5. HDOJ 1709 The Balance(母函数)

    The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. application/x-www-form-urlencoded multipart/form-data text/plain 的区别和作用

    我们知道在通过POST方式向服务器发送AJAX请求时最好要通过设置请求头来指定为application/x-www-form-urlencoded编码类型.知道通过表单上传文件时必须指定编码类型为&q ...

  7. javascript实现数据结构:串--堆分配存储表示

    堆分配存储表示 这种存储表示的特点是,仍以一组地址连续的存储单元存放串值字符序列,但它们的存储空间是在程序执行过程中动态分配而得. 结构图: 实现: function HString(){ this. ...

  8. 解决IE不支持position:fixed问题

    #box { /* 非IE6浏览器使用固定元素 */ position:fixed; top:0; left:0; /* IE6改为绝对定位,并通过css表达式根据滚动位置更改top的值 */ _po ...

  9. 如何在 Swift 语言下使用 iOS Charts API 制作漂亮图表?

    [编者按]本文作者 Joyce Echessa 是渥合数位服务创办人,毕业于台湾大学,近年来专注于协助客户进行 App 软体以及网站开发.文中作者通过示例介绍用 ios-charts 库创建简易美观的 ...

  10. LCA(最近公共祖先)离线算法Tarjan+并查集

    本文来自:http://www.cnblogs.com/Findxiaoxun/p/3428516.html 写得很好,一看就懂了. 在这里就复制了一份. LCA问题: 给出一棵有根树T,对于任意两个 ...