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