2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum(前缀和)
2017-05-23 15:20
513 查看
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 |∑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.
2≤n≤105
1≤2m≤n+1
|ai|,C≤104
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
官方题解:
前缀和排下序,最大前缀和-最小前缀和就是最大的区间和,然后依次枚举就好了
代码:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; typedef long long LL; const LL maxn=100010; LL sum[maxn]; int main() { LL n,m,C; while(~scanf("%I64d%I64d%I64d",&n,&m,&C)) { LL x; sum[0]=0; for(LL i=1;i<=n;++i) { scanf("%I64d",&x); sum[i]=sum[i-1]+x; } sort(sum,sum+n+1);//排序时要从0开始排序(去掉0的话就不能加上从1开始的任意整串) LL st=0,ed=n,ans=0; while(m--) { if(sum[ed]-sum[st]-C>0) ans+=sum[ed]-sum[st]-C,--ed,++st; else break; } printf("%I64d\n",ans); } return 0; }
相关文章推荐
- 2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum(前缀和)
- 2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum(前缀和)
- 2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum(前缀和)
- 2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum(前缀和)
- 2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum(前缀和)
- 2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum(前缀和)
- 2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum(前缀和)
- 2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum(前缀和)
- 2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum(前缀和)
- 2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum(前缀和)
- 2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum(前缀和)
- 2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum(前缀和)
- 2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum(前缀和)
- 2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum(前缀和)
- 2017ccpc全国邀请赛(湖南湘潭) H. Highway (最大生成树)(树的直径)
- 2017ccpc全国邀请赛(湖南湘潭) H. Highway (最大生成树)(树的直径)
- 2017ccpc全国邀请赛(湖南湘潭) H. Highway (最大生成树)(树的直径)
- 2017ccpc全国邀请赛(湖南湘潭) H. Highway (最大生成树)(树的直径)
- 2017ccpc全国邀请赛(湖南湘潭) H. Highway (最大生成树)(树的直径)
- 2017ccpc全国邀请赛(湖南湘潭) H. Highway (最大生成树)(树的直径)