Partial Sum
来源:互联网 发布:mac照片批量删除 编辑:程序博客网 时间:2024/04/28 14:50
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.
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 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 -14 2 1-1 2 2 -14 2 2-1 2 2 -14 2 10-1 2 2 -1
样例输出
3420
题目大意:给一个长度为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;}
阅读全文
0 0
- Partial Sum
- NYOJ927The partial sum problem
- The partial sum problem
- The partial sum problem
- The partial sum problem
- The partial sum problem
- NOOJ927The partial sum problem
- NYOJThe partial sum problem927
- The partial sum problem
- The partial sum problem
- Partial Sum (2017湘潭)
- The partial sum problem
- The partial sum problem
- NYOJ927 The partial sum problem
- NYOJ The partial sum problem
- nyoj927The partial sum problem(DFS)
- 【算法】Maximum Continuous Partial Sum
- DFS-The partial sum problem
- QT下简易画板实现
- nginx的那些事
- 状态压缩DP
- JavaScript 验证 API
- 玩转Google开源C++单元测试框架Google Test系列
- Partial Sum
- (多核DSP快速入门)8、利用MessageQ模块的多核灰度转换程序
- 入门专题-VUEJS
- No bean named 'shirofilter' available 报错
- 设计模式的应用场景(19)--访问者模式
- 最大公约数gcd()
- Java中函数参数是值还是对象的问题
- Day58-Oracle02
- QT 类及其实现效果(8)--橡皮筋线,Graphics view