题目链接

分析:

用 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 ;
}

POJ2479 Maximum sum(dp)的更多相关文章

  1. POJ2479 Maximum sum[DP|最大子段和]

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39599   Accepted: 12370 Des ...

  2. poj2479 Maximum sum

    http://poj.org/problem?id=2479 题目大意:给定一组n个整数:a ={a1, a2,…,我们定义一个函数d(a)如下: 你的任务是计算d(A).输入由T(<=30)测 ...

  3. POJ-2479 Maximum sum(动态规划)

    最大子序列和的加强版. 借助最大子序列和,分别正向和反向遍历一遍得到left和right数组(具体含义见代码注释) 然后再对left和right数组进行修正,保存从对应元素起向左或向右的最大连续和. ...

  4. URAL 1146 Maximum Sum(最大子矩阵的和 DP)

    Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...

  5. (线性dp 最大连续和)POJ 2479 Maximum sum

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 44459   Accepted: 13794 Des ...

  6. POJ 2479 Maximum sum(双向DP)

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36100   Accepted: 11213 Des ...

  7. 最大子矩阵和 URAL 1146 Maximum Sum

    题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...

  8. hdu-5586 Sum(dp)

    题目链接: Sum Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Probl ...

  9. ural 1146. Maximum Sum(动态规划)

    1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array of positive ...

随机推荐

  1. 3 Ways to Preload Images with CSS, JavaScript, or Ajax---reference

    Preloading images is a great way to improve the user experience. When images are preloaded in the br ...

  2. java 数据库两种连接方法

    package jdbc; import java.sql.*; public class ConnectionDemo2 { public static final String DBDRIVER= ...

  3. [转] Linux中gcc,g++常用编译选项

    http://blog.sina.com.cn/s/blog_5ff2a8a201011ro8.html gcc/g++ 在执行编译时,需要4步 1.预处理,生成.i的文件[使用-E参数] 2.将预处 ...

  4. MyEclipse 安装SVN插件方法及插件下载地址

    直接解压法 下载地址:http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240下载SVN插件:site-1.x.x. ...

  5. 常用 cmd 命令

    msconfig-------系统配置实用程序 mspaint--------画图板 devmgmt.msc--- 设备管理器 diskmgmt.msc---磁盘管理实用程序 services.msc ...

  6. 自己编写SqlHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  7. Adobe DreamweaverCS6安装及破解(序列号+破解补丁)

    一:安装 1) Adobe DreamweaverCS6中文版下载地址:Adobe DreamweaverCS6中文版下载 2)Adobe DreamweaverCS6安装及破解说明下载地址:Adob ...

  8. TSQL Challenge 2

    和之前发布的TSQL Challenge 1是同一系列的文章,看到那篇学习哪篇,没有固定的顺序,只为锻炼下思维. Compare rows in the same table and group th ...

  9. ExtJs API 下载以及部署

    ExtJs API 下载方法 1.进入sencha官网:https://www.sencha.com/ 2.点击“Docs”进入文档帮助页面:http://docs.sencha.com/ 3.点击左 ...

  10. 跟我学android-android常用布局介绍

    在上一章我们曾经谈到,Android平台的界面 是使用XML的方式设计的,然后在上一章我们只做了一个简单的界面,在这章,我们将介绍如何使用常用的控件设计实用的界面. Android中的视图都是继承Vi ...