zoukankan      html  css  js  c++  java
  • Codeforces Round #498 (Div. 3)

    You are given an array d1,d2,,dnd1,d2,…,dn consisting of nn integer numbers.

    Your task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (possibly, empty) of the original array.

    Let the sum of elements of the first part be sum1sum1 , the sum of elements of the second part be sum2sum2 and the sum of elements of the third part be sum3sum3 . Among all possible ways to split the array you have to choose a way such that sum1=sum3sum1=sum3 and sum1sum1 is maximum possible.

    More formally, if the first part of the array contains aa elements, the second part of the array contains bb elements and the third part contains cc elements, then:

    sum1=1iadi,sum1=∑1≤i≤adi, sum2=a+1ia+bdi,sum2=∑a+1≤i≤a+bdi, sum3=a+b+1ia+b+cdi.sum3=∑a+b+1≤i≤a+b+cdi.

    The sum of an empty array is 00 .

    Your task is to find a way to split the array such that sum1=sum3sum1=sum3 and sum1sum1 is maximum possible.

    Input

    The first line of the input contains one integer nn (1n21051≤n≤2⋅105 ) — the number of elements in the array dd .

    The second line of the input contains nn integers d1,d2,,dnd1,d2,…,dn (1di1091≤di≤109 ) — the elements of the array dd .

    Output

    Print a single integer — the maximum possible value of sum1sum1 , considering that the condition sum1=sum3sum1=sum3 must be met.

    Obviously, at least one valid way to split the array exists (use a=c=0a=c=0 and b=nb=n ).

    Examples
    Input
    Copy
    5
    1 3 1 1 4
    Output
    Copy
    5
    Input
    Copy
    5
    1 3 2 1 4
    Output
    Copy
    4
    Input
    Copy
    3
    4 1 2
    Output
    Copy
    0
    Note

    In the first example there is only one possible splitting which maximizes sum1sum1 : [1,3,1],[ ],[1,4][1,3,1],[ ],[1,4] .

    In the second example the only way to have sum1=4sum1=4 is: [1,3],[2,1],[4][1,3],[2,1],[4] .

    In the third example there is only one way to split the array: [ ],[4,1,2],[ ][ ],[4,1,2],[ ] .

    题意:将长度为n的集合划分为3个集合要求sum1==sum3,并且求出在满足条件的基础上sum1的最大值,sum1,sum2均可为空集。

    分析:我们可以sum1从前往后加,sum3从后往前加,从满足条件(sum1==sum3)中取最大值就OK。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include<algorithm>
     4 #include<iostream>
     5 #include<map>
     6 #include<stack>
     7 #include<cmath>
     8 typedef long long ll;
     9 using namespace std;
    10 const int MAXN=1e6+10;
    11 int str[MAXN];
    12 int  m,n;
    13 int main()
    14 {
    15     cin>>m;
    16     for(int i=0;i<m;i++)
    17     {
    18         cin>>str[i];
    19     }
    20     int l=1,r=m-2;
    21     ll kk=0;
    22     ll ans1=str[0],ans2=str[m-1];
    23     while(l<=r)
    24     {
    25         if(ans1<ans2)
    26         {
    27             ans1+=str[l++];
    28         }
    29         else if(ans2<ans1)
    30         {
    31             ans2+=str[r--];
    32         }
    33         else if(ans1==ans2)
    34         {
    35 
    36             kk=ans1;
    37             ans1+=str[l++];
    38             if(r>l)//在此一定判断一下是否越界,我死在这一次
    39             ans2+=str[r--];
    40         }
    41     }
    42     if(ans1==ans2&&m>1)//当m<2时不满足条件,ans=0
    43         kk=max(ans1,kk);
    44     cout<<kk<<endl;
    45 }
  • 相关阅读:
    js面试题-----算法类
    js面试题-----安全类
    js面试题-----通信类
    js面试题-----面向对象类
    js面试题-----HTTP协议类
    js面试题-----CSS盒模型
    Java-JVM 类的初始化
    加密的相关基础
    AngularJS-directive.js 基本指令
    AngularJS-liveRoomDirective.js 直播间指令
  • 原文地址:https://www.cnblogs.com/moomcake/p/9417474.html
Copyright © 2011-2022 走看看