zoukankan      html  css  js  c++  java
  • NOIP前夕:noi.openjudge,Maximum sum

    Maximum sum
    总Time Limit: 1000msMemory Limit: 65536kB
    Description
    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=s2
    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.

    求两块最大的互不相交的字序列的和
    f[i]表示以i结尾的最大子序列的和
    ff[i]表示i之前的最大子序列的和
    g[i]表示以i开头的最大子序列的和
    gg[i]表示i之后的最大子序列的和

    max=ff[i]+gg[i+1];

    code:
    var t:longint;
        init:array[1..50000]of longint;
        ii,i,j,k,n:longint;
        f,g,ff,gg:array[1..50000]of longint;
        max:longint;
        max1,max2:longint;
    
    function findmax(x,y:longint):longint;
             begin if x>y
                      then exit(x)
                      else exit(y);
             end;
    
    begin readln(t);
          for ii:=1 to t do
              begin readln(n);
                    for i:=1 to n do
                        read(init[i]);
                    f[1]:=init[1];
                    ff[1]:=f[1];
                    max1:=f[1];
                    for i:=2 to n do
                        begin f[i]:=findmax(f[i-1],0)+init[i];
                              if f[i]>max1
                                 then max1:=f[i];
                              ff[i]:=max1;
                        end;
                    g[n]:=init[n];
                    gg[n]:=init[n];
                    max2:=gg[n];
                    for i:=n-1 downto 1 do
                        begin g[i]:=findmax(g[i+1],0)+init[i];
                              if g[i]>max2
                                 then max2:=g[i];
                              gg[i]:=max2;
                        end;
                    max:=-maxlongint;
                    for i:=1 to n-1 do
                        if max<ff[i]+gg[i+1]
                           then max:=ff[i]+gg[i+1];
                    writeln(max);
              end;
    end.




  • 相关阅读:
    python数据类型-字典的练习
    python数据类型-元祖和字典类型、hash函数
    python数据类型-字符串
    python数据类型-列表练习
    python数据类型-列表
    iOS 熟悉CASharpLayer
    iOS 点击tableView的cell,让其滚到屏幕顶部
    iOS pop动画之弹性动画的基本使用
    iOS Json解析框架之MJExtension使用详解
    iOS pop动画之衰减动画的基本使用
  • 原文地址:https://www.cnblogs.com/spiderKK/p/4928469.html
Copyright © 2011-2022 走看看