前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >poj----Maximum sum(poj 2479)

poj----Maximum sum(poj 2479)

作者头像
Gxjun
发布2018-03-21 12:54:23
8710
发布2018-03-21 12:54:23
举报
文章被收录于专栏:ml

Maximum sum

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 30704

Accepted: 9408

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

代码语言:javascript
复制
1

10
1 -1 2 2 3 -3 4 -4 5 -5

Sample Output

代码语言:javascript
复制
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

题意:

给一个数列,求出数列中不相交的两个字段和,要求和最大。

思路:

对每一个i来说,求出【0~i-1】的最大子段和以及【i~n-1】的最大子段和,再加起来求最大的一个就行了。[0~i-1]的最大子段和从左向右扫描,【i~n-1】从右想左扫描

即可,时间复杂度O(n).

代码:

代码语言:javascript
复制
 1 #include<iostream>
 2 #include<cstdio>
 3 #define maxn 50001
 4 #include<algorithm>
 5 using namespace std;
 6 int  a[maxn];
 7 int left[maxn];
 8 int right[maxn];
 9 int max(int a,int b)
10 {
11     return a>b?a:b;
12 }
13 int main()
14 {
15     int t,i;
16     scanf("%d",&t);
17     while(t--)
18     {
19         int n;
20       scanf("%d",&n);
21       for(i=0;i<n;i++)
22           scanf("%d",&a[i]);
23       //此时::left【i】为包涵i最大字段和
24          ::left[0]=a[0];
25       for( i=1; i<n;i++)
26           if(::left[i-1]<0)
27               ::left[i]=a[i];
28           else
29               ::left[i]=::left[i-1]+a[i];
30      //此时left[i]为i左边最大字段和
31         for(i=1; i<n;i++)
32             ::left[i]=max(::left[i],::left[i-1]);
33             ::right[n-1]=a[n-1];
34         for(i=n-2;i>=0;i--)
35         {
36             if(::right[i+1]<0)
37                 ::right[i]=a[i];
38             else
39                 ::right[i]=::right[i+1]+a[i];
40         }
41         for(i=n-2;i>=0;i--)
42             ::right[i]=max(::right[i+1],::right[i]);
43         int res=-100000000;
44         for(i=1;i<n;i++)
45         {
46             res=max(res,::left[i-1]+::right[i]);
47         }
48         printf("%d\n",res);
49     }
50   return 0;
51 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-09-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档