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