1481:Maximum sum

总时间限制: 
1000ms

内存限制: 
65536kB
描述
Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:

                     t1     t2 
d(A) = max{ ∑ai + ∑aj | 1 <= s1 <= t1 < s2 <= t2 <= n }
i=s1 j=s2

Your task is to calculate d(A).

输入
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.
输出
Print exactly one line for each test case. The line should contain the integer d(A).
样例输入
1

10
1 -1 2 2 3 -3 4 -4 5 -5
样例输出
13
提示
In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer.

Huge input,scanf is recommended.

来源
POJ Contest,Author:Mathematica@ZSU
source:最大双子序列和,枚举i,分别统计i左边最大子序列和右边最大子序列。(改变一下代码风格)
 #include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int n,datanum,maxn;
int a[+],leftmax[+],rightmax[+];
int main()
{
int w,i;
scanf("%d",&datanum);
for (w=;w<=datanum;w++)
{
memset(a,,sizeof());
memset(leftmax,,sizeof(leftmax));
memset(rightmax,,sizeof(rightmax));
scanf("%d",&n);
for (i=;i<=n;i++)
scanf("%d",&a[i]);
leftmax[]=a[];
for (i=;i<=n;i++)
if (leftmax[i-]>) leftmax[i]=leftmax[i-]+a[i];
else leftmax[i]=a[i];
for (i=;i<=n;i++)
if (leftmax[i]<leftmax[i-]) leftmax[i]=leftmax[i-];
rightmax[n]=a[n];
for (i=n-;i>=;i--)
if (rightmax[i+]>) rightmax[i]=rightmax[i+]+a[i];
else rightmax[i]=a[i];
for (i=n-;i>=;i--)
if (rightmax[i]<rightmax[i+])
rightmax[i]=rightmax[i+];
maxn=-;
for (i=;i<n;i++)
maxn=max(maxn,leftmax[i]+rightmax[i+]);
printf("%d\n",maxn);
}
return ;
}

【noiOJ】p1481的更多相关文章

  1. 【noiOJ】P1996

    1996:登山 查看 提交 统计 提问 总时间限制:  5000ms 内存限制:  131072kB 描述 五一到了,PKU-ACM队组织大家去登山观光,队员们发现山上一个有N个景点,并且决定按照顺序 ...

  2. 【noiOJ】p1759

    1759:最长上升子序列 查看 提交 统计 提问 总时间限制:  2000ms 内存限制:  65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我 ...

  3. 【noiOJ】p6253

    t6253:用二分法求方程的根 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 用二分法求下面方程在(-10, 10)之间的一个根. 2x3- 4x2+ 3x ...

  4. 【noiOJ】p1794

    t1794:集合加法 查看 提交 统计 提问 总时间限制:  3000ms 内存限制:  65536kB 描述 给出2个正整数集合A = {pi | 1 <= i <= a},B = {q ...

  5. 【noiOJ】p1776

    t1776:木材加工 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 木材厂有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目是给 ...

  6. 【noiOJ】p8210

    10:河中跳房子 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 每年奶牛们都要举办各种特殊版本的跳房子比赛,包括在河里从一个岩石跳到另一个岩石.这项激动人心 ...

  7. 【noiOJ】p7939

    09:膨胀的木棍 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 当长度为L的一根细木棍的温度升高n度,它会膨胀到新的长度L'=(1+n*C)*L,其中C是热 ...

  8. 【noiOJ】p7914(..)

    08:不重复地输出数 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 输入n个数,从小到大将它们输出,重复的数只输出一次.保证不同的数不超过500个. 输入 ...

  9. 【noiOj】p8207(233)

    07:和为给定数 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 给出若干个整数,询问其中是否有一对数的和等于给定的数. 输入 共三行:第一行是整数n(0 & ...

随机推荐

  1. OCJP(1Z0-851) 模拟题分析(七)-->214

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

  2. Spring+Hibernate+Oracle中的Clob操作配置

    bean对象配置: <!-- 此处用于指定当前JDBC的实现,详见下面注解① --> <bean id="nativeJdbcExtractor" class=& ...

  3. Entity Framework Code First for SQL Compact

    这篇博客将介绍EF Code First中如何使用SQL Compact.SQL Compact是微软推出的免费的关系型数据库,目前最新版本是SQL Compact 4.0.微软的开发工具VS 201 ...

  4. 进程控制理论,王明学learn

    进程控制理论 一.进程 进程是一个具有一定独立功能程序的一次运行活动. 1.1进程特点 1.动态性:进程的实质是程序的一次执行过程,进程是动态产生,动态消亡的 2.并发性:任何进程都可以同其他进程一起 ...

  5. 网页或php服务连不上的几个可能原因

    1.webserver未启动. 2.php未启动. 3.url中端口和webserver配置文件中的不一致.

  6. HDU 5875 Function (2016年大连网络赛 H 线段树+gcd)

    很简单的一个题的,结果后台数据有误,自己又太傻卡了3个小时... 题意:给你一串数a再给你一些区间(lef,rig),求出a[lef]%a[lef+1]...%a[rig] 题解:我们可以发现数字a对 ...

  7. 在C#程序中实现插件架构

    阅读提示:这篇文章将讲述如何利用C#奇妙的特性,实现插件架构,用插件(plug-ins)机制建立可扩展的解决方案. 在.NET框架下的C#语言,和其他.NET语言一样提供了很多强大的特性和机制.其中一 ...

  8. canvas之2D上下文

    1.填充和描边 (1)fillStyle (2)strokeStyle 2.绘制矩形 (1)fillRect() (2)strokeRect() (3)clearRect()   :清除画布上的矩形区 ...

  9. 性能优化之Java(Android)代码优化

    最新最准确内容建议直接访问原文:性能优化之Java(Android)代码优化 本文为Android性能优化的第三篇——Java(Android)代码优化.主要介绍Java代码中性能优化方式及网络优化, ...

  10. 《DSP using MATLAB》示例Example4.10

    上代码: b = [1, 0.4*sqrt(2)]; a = [1, -0.8*sqrt(2), 0.64]; % compute the polynomials coefficients given ...