码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2479

时间:2014-11-15 17:01:43      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   ar   os   sp   for   strong   

Maximum sum
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 34180   Accepted: 10588

Description

Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:
bubuko.com,布布扣
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.

Source

POJ Contest,Author:Mathematica@ZSU
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define max(a,b) a>b?a:b
#define MAXV 50010
#define inf -10010

int lt[MAXV],rt[MAXV],a[MAXV],rtm[MAXV];

int main()
{
int t,n,i,temp;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);

temp=inf;lt[1]=a[1];rt[n]=a[n];
for(i=2;i<=n;i++){
    lt[i]=max(a[i],lt[i-1]+a[i]);
}
for(i=n-1;i>=1;i--){
rt[i]=max(a[i],rt[i+1]+a[i]);
}

rtm[n]=rt[n];
for(i=n-1;i>=1;i--)
    rtm[i]=max(rtm[i+1],rt[i]);

int ma=inf;
for(i=2;i<=n;i++){
    ma=max(ma,lt[i-1]+rtm[i]);
}
printf("%d\n",ma);

}
return 0;
}

  

poj 2479

标签:des   blog   http   io   ar   os   sp   for   strong   

原文地址:http://www.cnblogs.com/a972290869/p/4099522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!