【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 & ...
随机推荐
- attributeError:'module' object has no attribute ** 解决办法
写了一个小脚本,执行的时候报错: Traceback (most recent call last): File "F:/test/qrcode.py", line 109, in ...
- MySQL5.6 on Windows 安装失败: String was not recognized as a valid DateTime
在Win7,32位上安装MySQL5.6.21时出现日期/时间格式错误, 如下图所示: 错误描述为: String was not recognized as a valid DateTime. 下面 ...
- [LeetCode] Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- httpclient 4.5 get请求
还是官网靠谱啊 package com.test.httpclient.getpost; import java.io.IOException; import java.util.ArrayList; ...
- 登录成功,拿到token
历尽波折,终于成功登录并拿到了token: - (LoginResultDto *)login:(NSString *)userName andPassword:(NSString *)passwor ...
- 单图上传预览(uploadpreview )
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 用rlwrap使sqlplus可以上下翻页
下载rlwrap-0.30 从光盘上安装readline-devel和readline 安装rlwrap: #tar -zxvf rlwrap-0.30.tar.gz#cd rlwrap-0.30#. ...
- 关于三星I9305出现android.process.acore提示问题
背景:自己用百度云同步通讯录和用微信电话本删除联系人的时候总出现提示acore问题,为此上网找了许久. 网络上多说解决方案为:把Calendar.apk和CalendarProvider.apk两个文 ...
- Unity3D打Box游戏
先学习一些基本的脚本实现: 1.动态创建物体.默认位置是(0,0)位置 GameObject goNew = GameObject.CreatePrimitive(PrimitiveType.Cube ...
- OD附加功能分析
OD版本:OllyICE v1.10 在从文件菜单选择附加后,OD会在注册一个窗口类后,先创建一个0x138大小的进程表; 再是CreateWindowExA 创建窗口; 00478013 l ...