Partial Sum

Partial Sum

题目描述

Bobo has a integer sequence a1,a2,…,an of length n. Each time, he selects two ends 0≤l<r≤n and add into a counter which is zero initially. He repeats the selection for at most m times.

If each end can be selected at most once (either as left or right), find out the maximum sum Bobo may have.

输入

The input contains zero or more test cases and is terminated by end-of-file. For each test case:
The first line contains three integers n, m, C. The second line contains n integers a1,a2,…,an.
2≤n≤105 1≤2m≤n+1, |ai|,C≤104

The sum of n does not exceed 106.

输出

For each test cases, output an integer which denotes the maximum.

样例输入

4 1 1
-1 2 2 -1
4 2 1
-1 2 2 -1
4 2 2
-1 2 2 -1
4 2 10
-1 2 2 -1

样例输出

3
4
2
0

题目大意:给一个长度为n的数组,寻找l和r(每个l和r只能选择一次),l和r分别代表左右端点计算上面的公式的最大值,m代表最多计算次数(可以是零次),没计算一次sum就加上这次计算的值,找到一个最大的sum。

分析:相当于是计算最大子段和绝对值最大的一段,不止选一次并且选过的端点不能再选。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;

int main()
{
	ll a[100005]={0};
	ll n,m,c;
	while(scanf("%lld%lld%lld",&n,&m,&c)!=EOF){
		a[0]=0;
		for(int i=1;i<=n;i++){
			scanf("%lld",&a[i]);
			a[i]=a[i]+a[i-1];
		}
		sort(a,a+n+1);
        ll ans=0,temp;
        for(int i=0;i<m;i++){
            temp=abs(a[n-i]-a[i])-c;
            if(temp<=0)
				break;
            ans+=temp;
        }
        printf("%lld\n",ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值