Time Limit: 1 second

Memory Limit: 128 MB

【问题描述】

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

【输入格式】

输入文件maxsum1.in的第一行是一个正整数N,表示了序列的长度。 第2行包含N个绝对值不大于10000的整数A[i],描述了这段序列。

【输出格式】

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

【数据规模】

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

Sample Input1

7

2 -4 3 -1 2 -4 3

Sample Output1

4

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=u123

【题解】



设f[i]表示当i出现在最后的答案里且为最后一个数字时能够获取的最大和;

则有两种可能,只有这个数字本事作为答案,或者和之前连续的一段一起作为答案;则在a[i]和a[i]+f[i-1]之间取较大值;



【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second typedef pair<int,int> pii;
typedef pair<LL,LL> pll; void rel(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void rei(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} const int MAXN = 2e5+100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0); int n;
int f[MAXN],a[MAXN]; int main()
{
//freopen("D:\\rush.txt","r",stdin);
rei(n);
rep1(i,1,n)
rei(a[i]);
rep1(i,1,n)
f[i] = max(a[i],f[i-1]+a[i]);
int ans = f[1];
rep1(i,2,n)
ans = max(ans,f[i]);
printf("%d\n",ans);
return 0;
}

最新文章

  1. ASP.NET Web API Model-ModelBinder
  2. 相识从C语言开始
  3. git-quick-start 动画讲解Git命令行
  4. javascript显示实时时间
  5. yii2的windows下安装及前期步骤
  6. 了解Entity Framework中事务处理
  7. 继承Animation
  8. 在linux设置环境变量
  9. 5个可以帮你优化App的优秀网站
  10. WPF学习之资源-Resources
  11. Windows环境变量修改
  12. load Event
  13. SecureCRT中文显示乱码的解决方法
  14. jsp中包含JAVA代码
  15. php call_user_func和call_user_func_array
  16. SQL Server的备份
  17. Asp.net mvc 大文件上传 断点续传
  18. 通过非root用户访问VNC
  19. Qt 的一些浅知识点
  20. 20165235 实现pwd功能

热门文章

  1. 18.C语言多线程总结
  2. 1.5 Upgrading From Previous Versions官网剖析(博主推荐)
  3. golang filepath.Glob
  4. BZOJ3160: 万径人踪灭(FFT,回文自动机)
  5. Linux启动(续)
  6. Mysql学习总结(15)——Mysql错误码大全
  7. C/C++函数指针声明
  8. matlab 辅助函数 —— 文件下载与文件解压
  9. 学习笔记:_lodash.js常用函数
  10. django 简单会议室预约(2)