题目链接

分析:

用 d1[i] 表示左向右从0到i的最大连续和,d2[i] 表示从右向左, 即从n-1到i 的最大连续和。

ans = max(ans, d1[i]+d2[i+1]), i=0,1, 2,...,n-2

直接枚举会TLE, 优化下就可AC。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <cstring> using namespace std; const int maxn = +;
const int INF = (<<); int a[maxn], d1[maxn], d2[maxn]; int main() {
int T, n; scanf("%d", &T); while(T--){
scanf("%d", &n); for(int i=; i<n;i++) {
scanf("%d", &a[i]);
} d1[] = a[];
for(int i=; i<n; i++) {
if(d1[i-] >= ) d1[i] = a[i] + d1[i-];
else d1[i] = a[i];
} d2[n-] = a[n-];
for(int i=n-; i>=; i--) {
if(d2[i+] >= ) d2[i] = a[i] + d2[i+];
else d2[i] = a[i];
} int ans = -INF;
int maxx = d1[]; for(int i=; i<n-; i++) {
if(d1[i] > maxx) maxx = d1[i];
ans = max(ans, maxx + d2[i+]);
} printf("%d\n", ans);
} return ;
}

最新文章

  1. Game02 最新版本2.0.0
  2. Direct3D11学习:(二)基本绘图概念和基本类型
  3. js-string字符串对象
  4. HTML参考
  5. asp.net开源CMS推荐
  6. python多线程ctrl-c退出问题
  7. Debian下MySQL配置
  8. Android 获取JSP或ASP的sessionId(Cookie)
  9. update 改写 merge into
  10. 经验总结17--submitbutton,ajax提交
  11. AssetBundle的使用
  12. solr最佳实践
  13. rocketMq概念介绍
  14. C# 一款属于自己的音乐播放器
  15. k-近邻算法概述
  16. 每日一练ACM 2019.0418
  17. php面试题整理(二)
  18. AYUI第12个作品-英雄联盟-魔法少女的星光水晶2.0-WPF版本
  19. 移动端长按响应事件以及阻止默认行为e.preventDefault()导致定时器setTimeout不能响应
  20. spring boot 日志文件配置(logback-spring.xml)亲测可用!

热门文章

  1. QT程序库
  2. 深入分析 Java 中的中文编码问题--转
  3. Git学习之添加远程仓库
  4. android中百度地图定位的实现方法(仅适用于真机+WIFI联网环境)
  5. oracle批量转库工作,比较快捷的方式
  6. 创建 序列 存储过程 job
  7. java_annotation_02
  8. Java简介(3)-基本语法
  9. 页面d初始化加载
  10. 深入了解使用egret.WebSocket