2017湘潭赛Partial Sum

本文解析了一个关于整数序列的问题——PartialSum,旨在通过选择特定区间并计算其和,然后减去常数C来最大化总和。限制条件为每个端点只能使用一次,且操作次数有限。文章提供了一种解决方案,并附带了示例输入输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Partial Sum

Accepted : 155 Submit : 567
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

Source

XTU OnlineJudge
             http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1264

给一个序列,从中取一段,求和再减去C得到一个值,最多重复m次,求这些值的和(最大) ,每个端点只能取一次;

一道思维题  大神博客 http://blog.youkuaiyun.com/mengxiang000000/article/details/72228834

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll __int64
ll a[150000];
ll sum[150000];
int main()
{
    int n,m;
    ll C;
    while(~scanf("%d%d%I64d",&n,&m,&C))
    {
        for(int i=1;i<=n;i++)scanf("%I64d",&a[i]);
        for(int i=1;i<=n;i++)
        {
            if(i==1)sum[i]=a[i];
            sum[i]=sum[i-1]+a[i];
        }
        sort(sum,sum+1+n);
        ll cnt=0;
        ll ans=0;
        for(int i=0;i<m;i++)
        {
            cnt+=(sum[n-i]-sum[i]-C);
            ans=max(ans,cnt);
        }
        printf("%I64d\n",ans);
    }
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值