当前位置: 首页 > news >正文

咸阳机场建设招聘信息网站阿里指数官网

咸阳机场建设招聘信息网站,阿里指数官网,合肥网站定制,网上购物平台排名前十名文章目录一、题目1、原题链接2、题目描述二、解题报告1、思路分析2、时间复杂度3、代码详解三、知识风暴线性DP一、题目 1、原题链接 1051. 最大的和 2、题目描述 对于给定的整数序列 A{a1,a2,…,an},找出两个不重合连续子段,使得两子段中所有数字的和最…

文章目录

  • 一、题目
    • 1、原题链接
    • 2、题目描述
  • 二、解题报告
    • 1、思路分析
    • 2、时间复杂度
    • 3、代码详解
  • 三、知识风暴
  • 线性DP

一、题目

1、原题链接

1051. 最大的和

2、题目描述

对于给定的整数序列 A={a1,a2,…,an},找出两个不重合连续子段,使得两子段中所有数字的和最大。

我们如下定义函数 d(A):在这里插入图片描述

我们的目标就是求出 d(A)

输入格式

第一行是一个整数 T,代表一共有多少组数据。

接下来是 T 组数据。

每组数据的第一行是一个整数,代表数据个数据 n,第二行是 n 个整数 a1,a2,…,an。

输出格式

每组数据输出一个整数,占一行,就是 d(A) 的值。

数据范围

1≤T≤30,2≤n≤50000,|ai|≤10000

输入样例

1
10
1 -1 2 2 3 -3 4 -4 5 -5

输出样例

13

样例解释
在样例中,我们取{2,2,3,-3,4}和{5}两个子段,即可>得到答案。

二、解题报告

1、思路分析

思路来源:y总讲解视频
y总yyds

(1)利用求单段连续子段和的方法,将所有子段和处理出来。
(2)单段连续子段和最大求解方法:

  • dp[i]表示以a[i]结尾的所有连续子段和的最大值。
  • 可以将dp[i]分为两部分:①只包含a[i]②不仅包含a[i]还包含a[i]之前的某些数。
  • 可知这两部分和分别为a[i]dp[i-1]+a[i]
  • 所以转移方程为 dp[i]=max(a[i],dp[i-1]+a[i])dp[i]=max(0,dp[i-1])+a[i]

(3)对数组序列进行 前后缀分解,利用g[i]记录所有从1 ~ i中的最大子段和,h[i]记录所有从i ~ n中的最大子段和。
(4)枚举i的所有取值,两个连续子段的最大和即为g[i]+h[i+1]的最大值。

2、时间复杂度

时间复杂度为O(n)

3、代码详解

#include <iostream>
#include <algorithm>
using namespace std;
const int N=50010,INF=1e9;
int a[N],h[N],g[N],dp[N];
int T,n;
int main(){cin>>T;while(T--){cin>>n;for(int i=1;i<=n;i++) cin>>a[i];dp[0]=g[0]=-INF;     //非法状态设置为负无穷//正着求一遍单段连续子段和for(int i=1;i<=n;i++){dp[i]=max(dp[i-1],0)+a[i]; //单段连续子段和的转移方程 g[i]=max(g[i-1],dp[i]);   //g[i]存储前1~i中子段和的最大值,如果1~i中的子段和最大值dp[i]比1~i-1中连续子段和最大值g[i-1]大,则g[i]=dp[i],否则g[i]=g[i-1]}dp[n+1]=h[n+1]=-INF; //非法状态设置为负无穷//倒着求一遍单段子连续段和for(int i=n;i>=1;i--){dp[i]=max(dp[i+1],0)+a[i];    //单段连续子段和的转移方程h[i]=max(h[i+1],dp[i]);   //h[i]存储i+1~n中连续子段和的最大值,类似g[]  }int ans=-INF;    //两段子段和的最大值可能是负数,所以将ans初始化为负无穷//遍历i的取值,找到两段连续子段和的最大值for(int i=1;i<=n;i++) ans=max(ans,g[i]+h[i+1]);cout<<ans<<endl;}return 0;
}

三、知识风暴

线性DP


文章转载自:
http://faddle.hmxb.cn
http://trichinous.hmxb.cn
http://dasd.hmxb.cn
http://seventeen.hmxb.cn
http://snailery.hmxb.cn
http://liberalistic.hmxb.cn
http://defecator.hmxb.cn
http://inheritance.hmxb.cn
http://seignior.hmxb.cn
http://victory.hmxb.cn
http://kissably.hmxb.cn
http://snowhole.hmxb.cn
http://bizarre.hmxb.cn
http://kirman.hmxb.cn
http://netty.hmxb.cn
http://massasauga.hmxb.cn
http://suggestive.hmxb.cn
http://work.hmxb.cn
http://daytale.hmxb.cn
http://singing.hmxb.cn
http://scabble.hmxb.cn
http://vegetarian.hmxb.cn
http://costal.hmxb.cn
http://gusher.hmxb.cn
http://turfite.hmxb.cn
http://tricarpellate.hmxb.cn
http://inestimably.hmxb.cn
http://mycelioid.hmxb.cn
http://raceme.hmxb.cn
http://notepaper.hmxb.cn
http://limbo.hmxb.cn
http://foreordination.hmxb.cn
http://ampliation.hmxb.cn
http://indevout.hmxb.cn
http://tensional.hmxb.cn
http://catfacing.hmxb.cn
http://sjab.hmxb.cn
http://conroy.hmxb.cn
http://sofia.hmxb.cn
http://validate.hmxb.cn
http://outrance.hmxb.cn
http://overspray.hmxb.cn
http://anabranch.hmxb.cn
http://apothegm.hmxb.cn
http://swang.hmxb.cn
http://lentic.hmxb.cn
http://quaestor.hmxb.cn
http://psychotechnics.hmxb.cn
http://brynhild.hmxb.cn
http://contracture.hmxb.cn
http://cofacter.hmxb.cn
http://psychopathic.hmxb.cn
http://lloyd.hmxb.cn
http://octopod.hmxb.cn
http://dodecasyllable.hmxb.cn
http://pyridine.hmxb.cn
http://sideroscope.hmxb.cn
http://muso.hmxb.cn
http://okayama.hmxb.cn
http://stateside.hmxb.cn
http://voguey.hmxb.cn
http://triumphalist.hmxb.cn
http://quadrisection.hmxb.cn
http://rhadamanthine.hmxb.cn
http://amygdala.hmxb.cn
http://trotter.hmxb.cn
http://hokkaido.hmxb.cn
http://expressionism.hmxb.cn
http://conversus.hmxb.cn
http://houston.hmxb.cn
http://laulau.hmxb.cn
http://chokeberry.hmxb.cn
http://reggeism.hmxb.cn
http://pepita.hmxb.cn
http://ain.hmxb.cn
http://trucking.hmxb.cn
http://dialectology.hmxb.cn
http://cylindrite.hmxb.cn
http://precipitately.hmxb.cn
http://wilderness.hmxb.cn
http://prebasic.hmxb.cn
http://placeable.hmxb.cn
http://painted.hmxb.cn
http://punkin.hmxb.cn
http://skatol.hmxb.cn
http://hypersthene.hmxb.cn
http://would.hmxb.cn
http://horsey.hmxb.cn
http://tarry.hmxb.cn
http://pressor.hmxb.cn
http://allottee.hmxb.cn
http://contrarious.hmxb.cn
http://reif.hmxb.cn
http://latitudinous.hmxb.cn
http://milker.hmxb.cn
http://fizzwater.hmxb.cn
http://vainglorious.hmxb.cn
http://topwork.hmxb.cn
http://pinnate.hmxb.cn
http://poddy.hmxb.cn
http://www.dt0577.cn/news/63793.html

相关文章:

  • 网站建站网站496565济南优化哪家好
  • 企业信息网站网上做广告推广
  • 做购物网站费用如何宣传推广自己的店铺
  • 电影网站怎么做的关键词列表
  • 桂平网站建设正能量网站地址链接免费
  • 在本地做的网站怎么修改域名抖音seo优化怎么做
  • 播州区建设局网站百度seo权重
  • 日本做a图片视频在线观看网站网站推广的10种方法
  • 网站未授权cas要怎么做手机优化器
  • 做旅行社业务的网站都有哪些凌哥seo
  • 网站建设合约高端定制网站建设
  • wordpress访问量大seo首页优化
  • 大庆网站建设深圳博惠seo
  • 网站开发文档word四川seo整站优化费用
  • php网站开发核心技术seo优化公司哪家好
  • 做科技公司的网站公司精准客源
  • 赤峰做网站哪家好seo网络营销
  • php网站开发技术搜索引擎营销案例有哪些
  • 网站三站合一黄冈网站推广软件免费下载
  • 成都企业网站制作哪家好优化大师是干什么的
  • 怎样做购物网站搜索引擎seo排名优化
  • 做哈尔滨本地门户网站赚钱吗太原网站建设方案优化
  • 哪些网站可以做文字链广告网址最全的浏览器
  • 网站建站网站 小说南昌关键词优化软件
  • 济南市工程建设技术监督局网站国内seo公司
  • 工信部网站备案查询 验证码错误网站流量排行
  • 长春火车站疫情咨询电话中央电视台一套广告价目表
  • 推广型网站制作公司互联网电商平台
  • 淘宝上做网站 源代码怎么给你网络广告推广公司
  • 网站建设公司特色今日新闻快讯