【noiOJ】p1481
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=s2Your 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的更多相关文章
- 【noiOJ】P1996
1996:登山 查看 提交 统计 提问 总时间限制: 5000ms 内存限制: 131072kB 描述 五一到了,PKU-ACM队组织大家去登山观光,队员们发现山上一个有N个景点,并且决定按照顺序 ...
- 【noiOJ】p1759
1759:最长上升子序列 查看 提交 统计 提问 总时间限制: 2000ms 内存限制: 65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我 ...
- 【noiOJ】p6253
t6253:用二分法求方程的根 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 用二分法求下面方程在(-10, 10)之间的一个根. 2x3- 4x2+ 3x ...
- 【noiOJ】p1794
t1794:集合加法 查看 提交 统计 提问 总时间限制: 3000ms 内存限制: 65536kB 描述 给出2个正整数集合A = {pi | 1 <= i <= a},B = {q ...
- 【noiOJ】p1776
t1776:木材加工 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 木材厂有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目是给 ...
- 【noiOJ】p8210
10:河中跳房子 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 每年奶牛们都要举办各种特殊版本的跳房子比赛,包括在河里从一个岩石跳到另一个岩石.这项激动人心 ...
- 【noiOJ】p7939
09:膨胀的木棍 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 当长度为L的一根细木棍的温度升高n度,它会膨胀到新的长度L'=(1+n*C)*L,其中C是热 ...
- 【noiOJ】p7914(..)
08:不重复地输出数 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 输入n个数,从小到大将它们输出,重复的数只输出一次.保证不同的数不超过500个. 输入 ...
- 【noiOj】p8207(233)
07:和为给定数 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 给出若干个整数,询问其中是否有一对数的和等于给定的数. 输入 共三行:第一行是整数n(0 & ...
随机推荐
- HTTP中302与301的区别以及在ASP.NET中如何实现
一.官方说法301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于: 301 redirect: 301 代表永久性转移(Permanently Moved).302 re ...
- CSS中设置DIV垂直居中的N种方法 兼容IE浏览器
在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...
- win7Java开发环境配置
win7下Java开发环境的配置 首先下载符合操作系统版本的jdk,比如最新的jdk8: 下载链接:http://www.oracle.com/technetwork/java/javase/down ...
- golang channel buffer
package mainimport ( "fmt" "time")func main() { // Case-1: no buffer //chanMessa ...
- FreeSWITCH 体系配置结构
转自:http://www.cnblogs.com/logo-fox/archive/2013/12/09/3465440.html FreeSWITCH总体结构: FreeSWITCH 由一个稳定的 ...
- pythonchallenge之C++学习篇-00
前言 最近学习下C++,之前是python的用户,python解释器有诸多实现,其中最出名的要数C实现了,而且很多python的扩展模块可能要用C或者C++来写的,所以很有必要学习下C++了 为了避免 ...
- 分布式文件系统FastDFS设计原理(转)
FastDFS是一个开源的轻量级分布式文件系统,由跟踪服务器(tracker server).存储服务器(storage server)和客户端(client)三个部分组成,主要解决了海量数据存储问题 ...
- 【SQL 触发器】
一.MySQL上触发器的使用 示例: CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW t ...
- POJ 1182 食物链 (经典带权并查集)
第三次复习了,最经典的并查集 题意:动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们 ...
- Maven 项目导入错误解决。
Description Resource Path Location Type Failure to transfer org.apache.maven:maven-core:jar:2.0.6 fr ...