Partial Sum | ||
| Accepted : 4 | Submit : 12 | |
| Time Limit : 3000 MS | Memory Limit : 65536 KB | |
Partial SumBobo has a integer sequence a1,a2,…,an of length n. Each time, he selects two ends 0≤l<r≤n 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. InputThe 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.
OutputFor each test cases, output an integer which denotes the maximum. Sample Input4 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 Output3 4 2 0 |
思路:先统计前缀和,由于和取绝对值,所以可以直接进行排序,又每个点只能选一次,首尾两个指针同时向中间靠即可,遇到<C的直接退出。
# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
using namespace std;
const int N = 100030;
typedef long long LL;
LL a[N];
int main()
{
LL n, m, c, t;
while(~scanf("%I64d%I64d%I64d",&n,&m,&c))
{
a[0] = 0;
for(LL i=1; i<=n; ++i)
{
scanf("%I64d",&t);
a[i] = a[i-1] + t;
}
sort(a, a+1+n);
LL sum=0, cnt=0;
for(LL i=0,j=n; i<j&&cnt<m ; ++i,--j)
{
LL tmp = a[j]-a[i];
if(tmp >= c)
sum += tmp,++cnt;
else
break;
}
printf("%I64d\n",sum-cnt*c);
}
return 0;
}
本文探讨了一个关于整数序列的算法问题,旨在通过选择特定的子序列并计算其绝对值之和减去常数C的方式,找出最大的可能累加值。文章提供了详细的解决思路与C++代码实现。

被折叠的 条评论
为什么被折叠?



