zoukankan      html  css  js  c++  java
  • poj----Maximum sum(poj 2479)

    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

    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
    题意:
    给一个数列,求出数列中不相交的两个字段和,要求和最大。
    思路:
    对每一个i来说,求出【0~i-1】的最大子段和以及【i~n-1】的最大子段和,再加起来求最大的一个就行了。[0~i-1]的最大子段和从左向右扫描,【i~n-1】从右想左扫描
    即可,时间复杂度O(n).
    代码:
     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
    ",res);
    49     }
    50   return 0;
    51 }
    View Code
  • 相关阅读:
    async&await的前世今生
    如何使用cocos2dx-jsbinding 来处理分辨率适配
    cocos2d-x jsbinding 资源下载实现
    cocos2d-x jsbinding 在线更新策略设计
    xml2js
    快速入门cocos2d-x jsbinding
    cocos2d-x 工程目录结构说明
    Javascript 开发IDE
    认识cocos2d-x jsbinding
    MySQL 灵异事件一则 -- desc报语法错误
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3308402.html
Copyright © 2011-2022 走看看