XTU 1264 Partial Sum 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛(湖南)

Partial Sum

 
Accepted : 87 Submit : 366
Time Limit : 3000 MS Memory Limit : 65536 KB

Partial Sum

Bobo has a integer sequence a1,a2,,an of length n .Each time, he selects two ends 0l<rn and add |rj=l+1aj|C 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.

Input

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 .

  • 2n105
  • 12mn+1
  • |ai|,C104
  • The sum of n does not exceed 106 .

Output

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

Sample Input

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

Sample Output

3
4
2
0


解题思路:从0~n中选两个位置,例如3和7,那么 ans=ans+(a4+a5+a6+a7)-C,一直这样进行m次选取求最大的和

                  每次将其前缀和用一个数组存起来,那么a4+a5+a6+a7=sum[7]-sum[3]

                  对数组前缀和进行一次排序,,然后取m次那么就是其最大值

                  比赛的时候,没点到怎么优化,怎么交都是超时。。亏大发了

代码如下:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#define LL __int64
using namespace std;
const int maxn=100005;
LL sum[maxn];
int main()
{
    int n,m,c,a;
    while(scanf("%d %d %d",&n,&m,&c)!=EOF)
    {
        sum[0]=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a);
            sum[i]=sum[i-1]+a;///用数组sum储存前缀和
        }
        LL ans=0;
        sort(sum,sum+1+n);
        for(int i=0;i<m;i++)
        {
            int b=abs(sum[n-i]-sum[i])-c;
            if(b<=0) break;
            ans+=b;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值