Codeforces 33C. Wonderful Randomized Sum(贪心)

题目链接:http://codeforces.com/contest/33

Learn, learn and learn again — Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite that Valera is one of the best students, he failed to manage with the new homework. That's why he asks for your help. He has the following task. A sequence of n numbers is given. A prefix of a sequence is the part of the sequence (possibly empty), taken from the start of the sequence. A suffix of a sequence is the part of the sequence (possibly empty), taken from the end of the sequence. It is allowed to sequentially make two operations with the sequence. The first operation is to take some prefix of the sequence and multiply all numbers in this prefix by  - 1. The second operation is to take some suffix and multiply all numbers in it by  - 1. The chosen prefix and suffix may intersect. What is the maximum total sum of the sequence that can be obtained by applying the described operations?

Input

The first line contains integer n (1 ≤ n ≤ 105) — amount of elements in the sequence. The second line contains n integers ai( - 104 ≤ ai ≤ 104) — the sequence itself.

Output

The first and the only line of the output should contain the answer to the problem.

Examples
input
Copy
3
-1 -2 -3
output
Copy
6
input
Copy
5
-4 2 0 5 0
output
Copy
11
input
Copy
5
-1 10 -5 10 -2
output
Copy
18


题目大意:给你长度为n的序列,可以在前缀一起*-1或后缀一起*-1,问可能得到的最大序列和。

题目思路:贪心,对于当前点i,记录前面最大值和后面最大值即可。最大值?怎样才能得到当前的最大值,(1).1~i全部*-1有可能最大,(2)前面的某一截*-1+中间未*-1的有可能最大,前面某一截*-1,哪一截呢?就直接记录前面最大值(即是那某一截)+中间的和来更新。后缀最大一样的求法。最后for一遍更新一个ans即可。

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 0xffffffffffff;
const int mod = 1e9+7;
const int N = 1e6+5;

int a[N],b[N];
int presum[N],nextsum[N];  //记录*-1
int sum[N];
int premaxx[N],nextmaxx[N];


int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i],b[i]=-a[i];
    presum[0]=0;
    sum[0]=0;
    for(int i=1;i<=n;i++)
    {
        sum[i]=sum[i-1]+a[i];
        presum[i]=presum[i-1]+b[i];
    }
    nextsum[n+1]=0;
    for(int i=n;i>=1;i--)
        nextsum[i]=nextsum[i+1]+b[i];
    
    //求前缀最大
    int tidx,maxx;
    if(presum[1]>a[1])
    {
        maxx=presum[1];
        tidx=1;
        premaxx[1]=maxx;
    }
    else
    {
        maxx=0;
        tidx=0;
        premaxx[1]=a[1];
    }
    for(int i=2;i<=n;i++)
    {
        if(maxx+(sum[i]-sum[tidx])>maxx)   //判断第(2)中情况,即某一截*-1
        {
            premaxx[i]=maxx+(sum[i]-sum[tidx]);
        }
        if(presum[i]>maxx)       //前面一截全部*-1
        {
            tidx = i;
            maxx = presum[i];
            premaxx[i] = maxx;
        }
    }
    
    //求后缀最大
    if(nextsum[n]>a[n])
    {
        maxx=nextsum[n];
        tidx=n;
        nextmaxx[n]=maxx;
    }
    else
    {
        maxx=0;
        tidx=n+1;
        nextmaxx[n]=a[n];
    }
    for(int i=n-1;i>=1;i--)
    {
        if(maxx+(sum[tidx-1]-sum[i-1])>maxx)   //判断第(2)中情况,即某一截*-1
        { 
            nextmaxx[i]=maxx+(sum[tidx-1]-sum[i-1]);
        }
        if(nextsum[i]>maxx)     //后面一截全部*-1
        {

            tidx = i;
            maxx = nextsum[i];
            nextmaxx[i] = maxx;
        }
    }
    //得出ans
    int ans=nextmaxx[1];
    for(int i=1;i<n;i++)
        ans=max(ans,premaxx[i]+nextmaxx[i+1]);
    ans=max(ans,premaxx[n]);
    printf("%d\n",ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值