XTUOJ1264:Partial Sum(前缀和)
来源:互联网 发布:js中undefined null 编辑:程序博客网 时间:2024/06/05 04:51
Partial Sum
Accepted : 4 Submit : 12Time Limit : 3000 MS Memory Limit : 65536 KBPartial Sum
Bobo has a integer sequence
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
2≤n≤105 1≤2m≤n+1 |ai|,C≤104 - The sum of
n does not exceed106 .
Output
For each test cases, output an integer which denotes the maximum.
Sample Input
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
Sample Output
3420
思路:先统计前缀和,由于和取绝对值,所以可以直接进行排序,又每个点只能选一次,首尾两个指针同时向中间靠即可,遇到<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;}
0 0
- XTUOJ1264:Partial Sum(前缀和)
- XTOJ1264 Partial Sum 【前缀和】
- 2017 CCPC Partial Sum 前缀和
- 2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum(前缀和)
- XTU 1264 Partial Sum 【思维 + 前缀和排序】
- 17-JSCPC&&CCPC中南赛区 E-Partial Sum-区间求和&前缀和
- 523. Continuous Subarray Sum (前缀和)
- 部分和(partial sum)在算法求解中的作用
- HDU 4193 Non-negative Partial Sums(前缀和非负点个数,单调队列)
- Partial Sum
- HDU 5776 (sum 前缀和)
- CodeForces 608B Hamming Distance Sum(前缀和)
- hdu 5776 sum(前缀和取模)
- HDU 5776 sum(思维题+前缀和)
- The partial sum problem(dfs)
- Partial Sum(湘潭邀请赛E题(部分和问题))
- HDU 5776 sum(前缀和+取模)
- hdu 5776 Sum【前缀和相关】
- undefined和null的区别
- F
- 树
- 【数据结构】中的堆(HeapSort)排序
- 虚拟机搭建CDH-第六讲-安装mysql第一种方法(比较简单)
- XTUOJ1264:Partial Sum(前缀和)
- 《PCL点云库学习&VS2010(X64)》Part 37 FLANN——快速最近邻搜索库
- C++primer 第十二章笔记 初稿
- 学习笔记2-- Activity的什么鬼
- 烟台大学 oj 1006
- C#学习笔记 5 运算符
- mysql数据库(七)
- TCP/IP协议要点
- epoll模型之服务器设计