POJ 2479

来源:互联网 发布:nba中国官方软件 编辑:程序博客网 时间:2024/06/03 14:04
Maximum sum
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 30216 Accepted: 9236

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

1101 -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
简单DP:
b[i]表示以i为结尾的最大连续和 , c[i]表示以i为开头的最大连续和。
#include <cstdio>#include <cmath>#include <algorithm>#include <iostream>#include <cstring>#include <map>#include <string>#include <stack>#include <cctype>#include <vector>#include <queue>#include <set>using namespace std;#define MAXN 50000 + 10#define Inf 0x7ffffff#define eps 1e10-8#define mod 1000000007typedef long long LL;const double PI = acos(-1.0);typedef double D;//#define Online_Judge#define outstars cout << "***********************" << endl;#define REP(i , x , n) for(int i = x ; i < n ; i++)#define REPP(i , x , n) for(int i = x ; i <= n ; i++)#define PER(i , x , n) for(int i = x ; i > n ; i--)#define PERR(i ,x , n) for(int i = x ; i >= n ; i--)int a[MAXN] , T , n , b[MAXN] , c[MAXN];int main(){#ifdef Online_Judge    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);#endif // Online_Judge    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        REPP(i , 1 , n)        {            scanf("%d",&a[i]);        }        REPP(i ,1 , n)        {            if(i == 1)b[i] = a[i];            else            {                if(b[i - 1] > 0)b[i] = b[i - 1] + a[i];                else b[i] = a[i];            }        }        REPP(i , 2 , n)        {            if(b[i] < b[i - 1])b[i] = b[i - 1];        }        PERR(i , n , 1)        {            if(i == n)c[i] = a[i];            else            {                if(c[i + 1] > 0)c[i] = c[i + 1] + a[i];                else c[i] = a[i];            }        }        PERR(i , n - 1 , 1)        {            if(c[i] < c[i + 1])c[i] = c[i + 1];        }        int ans = b[1] + c[2];        REPP(i , 2 , n - 1)        {            if(ans < b[i] + c[i + 1])ans = b[i] + c[i + 1];        }        printf("%d\n",ans);    }    return 0;}/**/


原创粉丝点击