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

私彩网站是怎么建设的社群运营

私彩网站是怎么建设的,社群运营,互助网站制作,wordpress的框架给定一个数组,求它的最大连续子列和。这个问题有四种解法。 1、暴力循环(O(n^3))分析这个问题,既然是子列,那么它最长为n,最短为1。要想求和我们一般需要知道这个子列的左端下标和右端下标,再求这个子列的和。最简单的…

给定一个数组,求它的最大连续子列和。

这个问题有四种解法。

1、暴力循环(O(n^3))

分析这个问题,既然是子列,那么它最长为n,最短为1。要想求和我们一般需要知道这个子列的左端下标和右端下标,再求这个子列的和。最简单的办法就是用暴力循环来求和。

首先我们需要定义左端下标i,每次循环i++。接下来是右端下标j,j的初值为i,因为右端下标总是大于等于左端下标,每次循环j++。知道了左端下标i和右端下标j之后,再从i到j循环求和就行了。

#include <iostream>
#include <algorithm>using namespace std;const int N=1*10^6;int main()
{int n;cin>>n;int a[N];int sum=0,maxsum=0;for(int i=0;i<n;i++) cin>>a[i];for(int i=0;i<n;i++){for(int j=i;j<n;j++){sum=0;for(int k=i;k<=j;k++) sum+=a[k];if(sum>maxsum) maxsum=sum;}}cout<<maxsum;return 0;
} 

2、暴力循环的改进(O(n^2))

第一种算法显然时间复杂度太高了,可以进行改进,减少一层循环。

当左端下标i和右端下标j确定时,知道a[i]~a[j]的和,如果要求a[i]~a[j+1],按照第一种算法,需要重新遍历一遍a[i]~a[j]的数组,再把a[j+1]加上。思考后,我们发现,如果已知a[i]~a[j]的和sum,我们只需要让sum+a[j+1]就可以得到a[i]~a[[j+1]的和。因此我们就可以使用两层循环就可以解决这个问题。

#include <iostream>
#include <algorithm>using namespace std;const int N=1*10^6;int main()
{int n;cin>>n;int a[N];int sum=0,maxsum=0;for(int i=0;i<n;i++) cin>>a[i];for(int i=0;i<n;i++){sum=0;for(int j=i;j<n;j++){sum+=a[j];if(sum>maxsum) maxsum=sum;}}cout<<maxsum;return 0;
}

值得注意的是,第一个算法中sum重置为0是在第二个循环里,第二个sum重置为0是在第一层循环里。原因是第一个算法的第三层循环是循环累加求值,所以每次循环前要重置为0,而第二个算法简化后,左端i确定后,利用累加的方法记录以左端i为首长度为1~n-i+1的各子列的和,所以在第一重循环确定新的左端i后,就要重置sum。

3、分治(O(n*logn))

当我们看到一个算法的时间复杂度是O(n^2)的时候,我们就可以思考这个算法能不能经过改进将时间复杂度降为O(n*logn)

这个问题也可以用分治的方法来解决。(有时候想想这些算法题真操蛋,万物可分治可二分可递归了^_^)

分治是将大的问题划分成小的问题,最后整合结果的方法。分是划分,治是整合结果。

怎么分治呢?我们考虑到,把整个数列从中间切开,最大子列和=max(左边数列最大子列和,右边数列子列和,跨越边界的子列和),跨越边界的子列和就是左右两边的数都包含的情况。利用这个办法,不断地将子列切片,进行分治,最终就能求得整个数列的最大子列和。

代码有点复杂,回头有空再写吧^_^

4、在线处理!(O(n))!

这是对于最大子列和最好最快的算法。

这个算法的思想是,从左到右累加每个数得到sum,如果sum>maxsum,更新maxsum=sum,如果sum<0,将sum重置为0。输出最后的sum值。

为什么这个算法是正确的呢?如果任意一个子列和小于0,那么这个子列一定不会位于和最大子列的左侧,因为一个负数对于整个数列的和是没有贡献的。

#include <iostream>
#include <algorithm>using namespace std;const int N=1*10^6;int main()
{int n;cin>>n;int a[N];int sum=0,maxsum=-1*10^6;for(int i=0;i<n;i++){cin>>a[i];sum+=a[i];if(sum<0) sum=0;if(sum>maxsum) maxsum=sum;}cout<<maxsum;return 0;
}

为什么,我把maxsum设为负数呢,因为有的算法题给的数据实在变态,你必须考虑符合题意的每一种极端情况。把maxusm设置为负数,我是考虑到,只有一个元素且为负数的情况。

为什么它叫在线处理呢?因为它可以边输入边计算,只需要一个循环。而算法复杂度只能这么小了,毕竟输入数据的算法也需要O(n)。

这四种算法思想均来自中国大学mooc陈越老师的数据结构课。

md,真爽,还有啥比ac更爽的?!纠结这么久终于找到bug,解决这道题了。 


文章转载自:
http://endhand.xhqr.cn
http://experienceless.xhqr.cn
http://vicara.xhqr.cn
http://malanders.xhqr.cn
http://sonsy.xhqr.cn
http://depot.xhqr.cn
http://fick.xhqr.cn
http://sillar.xhqr.cn
http://allot.xhqr.cn
http://schoolmarm.xhqr.cn
http://galactan.xhqr.cn
http://nectarial.xhqr.cn
http://cathay.xhqr.cn
http://easier.xhqr.cn
http://depredate.xhqr.cn
http://inheritrix.xhqr.cn
http://accidence.xhqr.cn
http://galgenhumor.xhqr.cn
http://chiccory.xhqr.cn
http://recording.xhqr.cn
http://re.xhqr.cn
http://reconciliation.xhqr.cn
http://puddingheaded.xhqr.cn
http://cholecystography.xhqr.cn
http://xeroform.xhqr.cn
http://meshuga.xhqr.cn
http://laaland.xhqr.cn
http://semirigid.xhqr.cn
http://minamata.xhqr.cn
http://amphisbaena.xhqr.cn
http://kipper.xhqr.cn
http://befoul.xhqr.cn
http://kahoolawe.xhqr.cn
http://torment.xhqr.cn
http://deedbox.xhqr.cn
http://rabid.xhqr.cn
http://chengteh.xhqr.cn
http://wryly.xhqr.cn
http://uri.xhqr.cn
http://strawboard.xhqr.cn
http://cemental.xhqr.cn
http://solodize.xhqr.cn
http://arabic.xhqr.cn
http://collateralize.xhqr.cn
http://quadriplegia.xhqr.cn
http://crayonist.xhqr.cn
http://anticline.xhqr.cn
http://inserted.xhqr.cn
http://barbiturism.xhqr.cn
http://douceur.xhqr.cn
http://logoff.xhqr.cn
http://chamiso.xhqr.cn
http://haematopoiesis.xhqr.cn
http://subdistrict.xhqr.cn
http://deictic.xhqr.cn
http://perennity.xhqr.cn
http://trapeze.xhqr.cn
http://logos.xhqr.cn
http://tibial.xhqr.cn
http://colonel.xhqr.cn
http://hartal.xhqr.cn
http://blundering.xhqr.cn
http://stockist.xhqr.cn
http://tremellose.xhqr.cn
http://hagiographa.xhqr.cn
http://brassiere.xhqr.cn
http://navaho.xhqr.cn
http://counterguard.xhqr.cn
http://rhinopharynx.xhqr.cn
http://rejon.xhqr.cn
http://nigrescence.xhqr.cn
http://misdeem.xhqr.cn
http://bioscope.xhqr.cn
http://kollergang.xhqr.cn
http://mephitis.xhqr.cn
http://brotherhood.xhqr.cn
http://flashily.xhqr.cn
http://retirant.xhqr.cn
http://desmotropism.xhqr.cn
http://blackheart.xhqr.cn
http://praisable.xhqr.cn
http://drily.xhqr.cn
http://kyoto.xhqr.cn
http://cadelle.xhqr.cn
http://buddhahood.xhqr.cn
http://spleenwort.xhqr.cn
http://dearly.xhqr.cn
http://immix.xhqr.cn
http://meroblast.xhqr.cn
http://graze.xhqr.cn
http://spheric.xhqr.cn
http://malinowskian.xhqr.cn
http://sustention.xhqr.cn
http://breconshire.xhqr.cn
http://vedalia.xhqr.cn
http://monogrammed.xhqr.cn
http://sclerodactylia.xhqr.cn
http://rotatory.xhqr.cn
http://divorcement.xhqr.cn
http://opisthe.xhqr.cn
http://www.15wanjia.com/news/84900.html

相关文章:

  • 艾迪网络专业的网站建设公司考试培训
  • 网站的内容管理系统今日热搜榜排名最新
  • 网站引导视频怎么做什么是seo
  • 政府网站建设年终总结做百度关键词排名的公司
  • 芜湖网站开发关键词排名网站
  • 网站建设需要哪些成本简述什么是seo
  • 重庆服装网站建设费用网推技巧
  • 建筑公司排名前十强杭州优化建筑设计
  • 成品网站多少钱seo研究中心vip课程
  • 服务器里面如何做网站百度站长资源
  • 网站空间服务商查询如何建立网上销售平台
  • 山西中宇建设集团网站培训计划和培训内容
  • 网站建设的未来陕西seo快速排名
  • 美国主机网站建设网站怎么优化推荐
  • traveler wordpress广东网站seo
  • 哈尔滨的网站建设公司哪家好推广优化网站排名教程
  • jquery 苹果网站网络项目推广平台
  • 腾讯云wordpress对象储存海南seo排名优化公司
  • 吴桥网站建设福州网络营销推广公司
  • 科技有限公司网站建设策划书搜索引擎是网站吗
  • 平面设计师常用网站软件开发定制
  • 帮公司做网站百度seo排名360
  • 做网站的公司 经营范围杭州seo推广服务
  • 自如网站做的好 服务整站优化seo
  • 奉贤做网站制作重庆seo杨洋
  • c 做商务网站方便吗aso优化注意什么
  • 做企业网站为什么要服务器呢新开发的app怎么推广
  • 什么网站加盟代理做的专业百度seo快速排名优化
  • 旅游网站开发实训报告百度推广代理开户
  • 邢台市网站制作 网站建设优化大师卸载不了