https://www.luogu.org/problem/show?pid=1115#sub

题目描述

给出一段序列,选出其中连续且非空的一段使得这段和最大。

输入输出格式

输入格式:

输入文件maxsum1.in的第一行是一个正整数N,表示了序列的长度。

第2行包含N个绝对值不大于10000的整数A[i],描述了这段序列。

输出格式:

输入文件maxsum1.out仅包括1个整数,为最大的子段和是多少。子段的最小长度为1。

输入输出样例

输入样例#1:

7
2 -4 3 -1 2 -4 3
输出样例#1:

4

说明

【样例说明】2 -4 3 -1 2 -4 3

【数据规模与约定】

对于40%的数据,有N ≤ 2000。

对于100%的数据,有N ≤ 200000。

 #include <algorithm>
#include <cstdio> using namespace std; int n,cnt,dp,f,num[]; int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&num[i]);
if(num[i]<) cnt++;
if(cnt==n)
{
sort(num+,num+n+);
printf("%d",num[n]);
return ;
}
} f=dp=num[];
for(int i=;i<=n;i++)
{
if(f<) f=num[i];
else f+=num[i];
if(f>dp) dp=f;
}
printf("%d",dp);
return ;
}

最新文章

  1. ios系统(苹果手机)按钮显示为圆角和渐变的问题
  2. Ajax方法封装
  3. Edit Distance编辑距离(NM tag)- sam/bam格式解读进阶
  4. php练习6——面向对象编程(打印乘法表)
  5. Linux shell入门基础(四)
  6. DEDECMS批量修改默认文章和列表命名规则的方法
  7. linux shell 不同进制数据转换(二进制,八进制,十六进制,base64) (转)
  8. nginx如何配置网页错误页面
  9. 51nod 1008 N的阶乘 mod P
  10. MySQL数据库学习三 数据库对象和基本操作
  11. [HCNA]VLAN配置Trunk接口
  12. Python 官方文档解读(2):threading 模块
  13. 我所知道的几种display:table-cell的应用
  14. Lua 可变参数之arg与select
  15. Java时代即将来临
  16. exchange 2010
  17. &lt;OFFER03&gt;03_01_DuplicationInArray
  18. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) B. Problems for Round 水题
  19. Spring MVC隐藏字段域
  20. 交叉熵(Cross-Entropy) [转载]

热门文章

  1. SRM 622 D2L3: Subsets, math, backtrack
  2. hdu 5411 CRB and Puzzle 矩阵高速幂
  3. Uva1335 二分+贪心
  4. friend(hdoj 1719)
  5. php word转pdf
  6. LeetCode Weekly Contest 27
  7. Hadoop MapReduce编程 API入门系列之二次排序(十六)
  8. 对比JavaScript的入口函数和jQuery的入口函数
  9. Struts2框架学习(三)——配置详解
  10. 图片加载AsyncTask并发问题