Maximum sum

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 39089   Accepted: 12221

Description

Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:

Your task is to calculate d(A).

Input

The input consists of T(<=30) test cases. The number of test cases (T) is given in the first line of the input. 
Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, ..., an. (|ai| <= 10000).There is an empty line after each case.

Output

Print exactly one line for each test case. The line should contain the integer d(A).

Sample Input

1

10
1 -1 2 2 3 -3 4 -4 5 -5

Sample Output

13

Hint

In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer.

Huge input,scanf is recommended.

分别求出两端开始的最大子段和,然后枚举左右两段的分界,找出最大值。

 //2016.8.21
#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; const int N = ;
const int inf = 0x3f3f3f3f;
int a[N], dpl[N], dpr[N];//dpl[i]表示从左往右到第i位的最大子段和,dpr[i]表示从右往左到第i位的最大子段和 int main()
{
int T, n;
cin>>T;
while(T--)
{
scanf("%d", &n);
for(int i = ; i < n; i++)
{
scanf("%d", &a[i]);
}
memset(dpl, , sizeof(dpl));
memset(dpr, , sizeof(dpr));
//从左往右扫
//*************************************************
dpl[] = a[];
for(int i = ; i < n; i++)
if(dpl[i-]>) dpl[i] = dpl[i-]+a[i];
else dpl[i] = a[i];
for(int i = ; i < n; i++)
if(dpl[i]<dpl[i-])
dpl[i] = dpl[i-];
//从右往左扫
//*************************************************
dpr[n-] = a[n-];
for(int i = n-; i>=; i--)
if(dpr[i+]>) dpr[i] = dpr[i+]+a[i];
else dpr[i] = a[i];
for(int i = n-; i>=; i--)
if(dpr[i]<dpr[i+])
dpr[i] = dpr[i+];
//*************************************************
int ans = -inf;
for(int i = ; i < n-; i++)
{
ans = max(ans, dpl[i]+dpr[i+]);
}
cout<<ans<<endl;
} return ;
}

POJ2479(dp)的更多相关文章

  1. POJ2479【DP 枚举】

    题意:给出一串数字,求出其中不重不交的两个子串的和的最大值 思路:最近最大子串和做多了,感觉这题有点水.枚举分割点,将序列分成左右两串,然后看左右串的最大子串和的最大值. //poj2479 #inc ...

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

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

  3. poj2479(dp)

    题目链接:http://poj.org/problem?id=2479 题意:求所给数列中元素值和最大的两段子数列之和. 分析:从左往右扫一遍,b[i]表示前i个数的最大子数列之和. 从右往左扫一遍, ...

  4. 最长子序列dp poj2479 题解

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 44476   Accepted: 13796 Des ...

  5. POJ2479 Maximum sum(dp)

    题目链接. 分析: 用 d1[i] 表示左向右从0到i的最大连续和,d2[i] 表示从右向左, 即从n-1到i 的最大连续和. ans = max(ans, d1[i]+d2[i+1]), i=0,1 ...

  6. POJ2479,2593: 两段maximum-subarray问题

    虽然是两个水题,但是一次AC的感觉真心不错 这个问题算是maximum-subarray问题的升级版,不过主要算法思想不变: 1. maximum-subarray问题 maximum-subarra ...

  7. 跟着大佬重新入门DP

    数列两段的最大字段和 POJ2479 Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41231 Acce ...

  8. DP的学习

    DP在ACM的算法里面可算是重中之重,题目类型千变万化,题目难度差异也很大.是一种很讲究技巧的算法,而且代码实现相对容易,1y率非常高(除有些bt数据外).总之DP就是一向非常重要,又非常博大精深的算 ...

  9. DP入门练习

    T1 题目:codevs4815江哥的dp题a codevs4815 一个简单的DP,注意开long long(不然会全WA),以及初始条件(这题有负数,所以要把f设成极小值.还要保证转移正确). # ...

随机推荐

  1. HDU 1828 POJ 1177 Picture

    矩形周长并 POJ上C++,G++都能过,HDU上C++过了,G++WA ,不知道为什么 #include<cstdio> #include<cstring> #include ...

  2. 计算机学院大学生程序设计竞赛(2015’12) 1003 The collector’s puzzle

    #include<cstdio> #include<algorithm> using namespace std; using namespace std; +; int a[ ...

  3. BNU 51002 BQG's Complexity Analysis

    模拟 #include<cstdio> #include<cstring> #include<cmath> #include<vector> #incl ...

  4. C# 开发系列(三)

    参考:http://stackoverflow.com/questions/11248935/passing-values-to-a-put-json-request-in-c-sharp 发送htt ...

  5. 浅谈mysql主从复制的高可用解决方案

    1.熟悉几个组件(部分摘自网络)1.1.drbd     —— DRBD(Distributed Replicated Block Device),DRBD号称是 "网络 RAID" ...

  6. Linux Apache2 配置介绍

    转自:http://blog.csdn.net/hursing/article/details/18730813 apache原指http server程序,后来成为了该程序的组织名,所以把原程序名定 ...

  7. [即时通讯]openfire启动失败解决办法

    当你发现你的电脑上的openfire无论你是重新安装还是重启都无法启动的情况下你可以按照我下面写的那些终端指令来启动你的openfire不用再去重新做你的系统了. 一般你发现你的openfire打开出 ...

  8. 让程序同时只能运行一个C++ Builder实现(转)

    源:让程序同时只能运行一个 很多人都讨论过这个问题, 这里用Victor串口控件里面现成的共享内存功能来实现. 当程序运行第二次时只是激活第一次运行的窗口, 而不是再运行一个程序. 需要在主程序里实现 ...

  9. NPOI使用Datatable导出到Excel

    首先要引用dll 下载地址:http://pan.baidu.com/s/1dFr2m 引入命名空间: using NPOI.HSSF.UserModel;using NPOI.SS.UserMode ...

  10. JDBC操作数据时中文乱码

    /** * DB地址 */ private static final String DB_URL="jdbc:mysql://localhost:3306/db_book?useUnicod ...