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. Basic knowledge of html (keep for myself)

    1. 通常标签 <strong> 替换加粗标签 <b> 来使用, <em> 替换 <i>标签使用. 2. 在 <head>元素中你可以插入脚 ...

  2. iis express 启动多个网站

      iis express一次只能运行一个网站,  执行iisexpress 不加参数. 将执行配置文件中的第一个网站. iis express一次只能运行一个 应用程序池. 可以使用这个特点实现一次 ...

  3. js实用功能

    //日期格式转换 Date.prototype.format = function (format) {     /*      * eg:format="yyyy-MM-dd hh:mm: ...

  4. uitableviewcell 和 uibutton

    如果cell上面只有一个button  可以设置button.tag=IndexPath.Row;得到当前点击的行数,设置button属性的时候,可以设置一个全局的button来记住当前点击的butt ...

  5. BZOJ2435: [Noi2011]道路修建

    这种水题真是……没一次AC都不好意思见人啊 P.S. LINUX无限栈真是爽炸了… 我爱递归 /**************************************************** ...

  6. 利用MariaDB Galera Cluster实现mariadb的多主复制

    一.MariaDB Galera Cluster概要: .简述: MariaDB Galera Cluster 是一套在mysql innodb存储引擎上面实现multi-master及数据实时同步的 ...

  7. PHP JAVA Bridge桥的最新使用

    PHP JAVA Bridge桥的最新使用 在PHP和Java之间搭建一座桥梁,利用这座桥梁在这两个实体之间建立起一个沟通渠道,在这座桥梁的帮助下,你可以在Java中开发类,然后在PHP中调用它们的方 ...

  8. 关于trello的分享

    https://www.the5fire.com/trello-share.html 昨天在公司跟同事分享了关于trello的使用,这里也分享给大家.比较简单,重点是让你知道有这么个东西.tangle ...

  9. 历代诗词咏宁夏注释2_----苍岩道人<登文昌阁>

    登文昌阁[1] 苍岩道人 壮年碌碌走尘埃,此地清幽不肯来. 老去始惊春梦促,韶光易过槿花开.[2] 历朝兴废书千卷,万古忠奸土一堆.[3] 惟爱莎罗歌最好,闲时拍板满斟杯.[4] 注释 [说明]选自& ...

  10. Windows启动系统程序命令

    DEVMGMT.MSC - Device Manager 设备管理器 DISKMGMT.MSC - Disk Management 磁盘管理   WindowsXP常用命令http://baike.b ...