Partial Sum
Accepted : Submit :
Time Limit : MS Memory Limit : KB Partial Sum Bobo has a integer sequence a1,a2,…,an of length n. Each time, he selects two ends ≤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. ≤n≤
≤2m≤n+
|ai|,C≤
The sum of n does not exceed . Output For each test cases, output an integer which denotes the maximum.
Sample Input - - - - - - - - Sample Output Source
XTU OnlineJudge /**
题目:Partial Sum
链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1264
题意:给n个数,每次操作选择一个L,一个R,表示区间左右端点,该操作产生的贡献为[L+1,R]的和的绝对值-C。
0<=L<R<=n; 如果选过L,R这两个位置,那么以后选择的L,R都不可以再选择这两个位置。最多操作m次,求可以获得的
最大贡献和。
思路:脑洞。原公式可以转化为|sum[r]-sum[l]|-c,sum[i]表示前i项的前缀和。
由于绝对值的影响,所以对前缀和排序,然后l从左边开始递增枚举,r从右边开始递减枚举,每次组成一对(sum[l],sum[r])作为贡献计算。 */ #include<bits/stdc++.h> using namespace std;
typedef long long LL;
const int maxn = 1e5+;
int a[maxn], sum[maxn];
int main()
{
int n, m, c;
while(scanf("%d%d%d",&n,&m,&c)!=EOF)
{
//cout<<"yes"<<endl;
for(int i = ; i <= n; i++) scanf("%d",&a[i]);
sum[] = ;
for(int i = ; i <= n; i++){
sum[i] = a[i]+sum[i-];
}
sort(sum,sum++n);
int l = , r = n;
LL ans = ;
while(m--){
int value = abs(sum[r]-sum[l]);
if(value<=c) break;
ans += value-c;
l++, r--;
}
printf("%lld\n",ans);
}
return ;
}

2017-5-14 湘潭市赛 Partial Sum 给n个数,每次操作选择一个L,一个R,表示区间左右端点,该操作产生的贡献为[L+1,R]的和的绝对值-C。 0<=L<R<=n; 如果选过L,R这两个位置,那么以后选择的L,R都不可以再选择这两个位置。最多操作m次,求可以获得的 最大贡献和。的更多相关文章

  1. Partial Sum

    Partial Sum Accepted : 80   Submit : 353 Time Limit : 3000 MS   Memory Limit : 65536 KB  Partial Sum ...

  2. 【2017 ICPC亚洲区域赛北京站 J】Pangu and Stones(区间dp)

    In Chinese mythology, Pangu is the first living being and the creator of the sky and the earth. He w ...

  3. 2017.10.14 Java的流程控制语句switch&&随机点名器

    今日内容介绍 1.流程控制语句switch 2.数组 3.随机点名器案例 ###01switch语句解构     * A:switch语句解构       * a:switch只能针对某个表达式的值作 ...

  4. HihoCoder 1629 Graph (2017 ACM-ICPC 北京区域赛 C题,回滚莫队 + 启发式合并 + 可撤销并查集)

    题目链接  2017 ACM-ICPC Beijing Regional Contest Problem C 题意  给定一个$n$个点$m$条边的无向图.现在有$q$个询问,每次询问格式为$[l, ...

  5. 2017南开ACM校赛(网络赛) 民间题解

    orz 首先说一下这个只是民间题解,可能会有很多错误 程序还没有评测,所以可能存在问题 C题比赛的时候没想到..后来发现是个模板题,所以没有代码 希望这份题解能对读者有所启发吧... A题 直接倒序枚 ...

  6. NYOJ 927 The partial sum problem 【DFS】+【剪枝】

    The partial sum problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 One day,Tom's girlfriend give him a ...

  7. 部分和(partial sum)在算法求解中的作用

    C++ 的 STL 库的 <numeric> 头文件的 partial_sum 函数已实现了对某一序列的 partial sum. partial_sum(first, last, des ...

  8. ACM题目————The partial sum problem

    描述 One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choo ...

  9. NYOJ--927--dfs--The partial sum problem

    /* Name: NYOJ--927--The partial sum problem Author: shen_渊 Date: 15/04/17 19:41 Description: DFS,和 N ...

随机推荐

  1. 将html文档转成pdf

    (1)使用场景:在项目中使用到了合同,只有在合同的头部,是不相同的.在合同的主体部分都是相同的,因此就把他放到了模板(html文件)里面. 在用户线上签约完成之后,可以将pdf版的合同下载. (2)需 ...

  2. Swift开发经验——外部参数名

    一.什么是外部参数名? 浅显地说,外部参数名就是在调用一个方法时要在方法的参数前面加上一个特定的名字,目的是便于阅读代码,提高维护效率.   二.在最新的Xcode中,外部参数名的性质与用法如下 性质 ...

  3. Matlab自带的曲线拟合程序

    这个函数的功能是能自动搜索参数的取值,从而使得方程的误差最小. 效果如下 代码如下 %% Optimal Fit of a Non-linear Function % This is a demons ...

  4. Node.js 连接mySQL程序

    环境:Oracle Enterprise Linux R5U7 安装mySQL 关于离线安装,下次在尝试,目前先来在线安装,过程如下: $ rpm -qa | grep -i mysql $ wget ...

  5. Xamarin.Forms 调用 腾讯地图SDK

    Xamarin.Forms研究了好一段时间了,最近一直在学习中,想尝试一下调用其他的SDK,就如腾讯地图SDK(申请容易). 完成此次项目得感谢以下链接: http://www.cnblogs.com ...

  6. servlet之request和response的使用区分

    有的时候在写servlet程序时,我总是被一个方法该用request去调用.还是用response去调用而困惑.从而造成编程时间的延长. 我在区分request和response的使用时,使用的方法是 ...

  7. Linux内核regulator架构和编写

    电源种类介绍 (百度百科)LDO是low dropout regulator,意为低压差线性稳压器,是相对于传统的线性稳压器来说的.传统的线性稳压器,如78xx系列的芯片都要求输入电压要比输出电压高出 ...

  8. javascript快速入门5--数组与对象

    数组 数组,实际上就是将一大堆相似的数据有秩序的放在格子箱中,十分像药房里的那些柜子. 数据1 数据2 数据3 数据4 数据5 数据6 用代码创建数组 var arr = new Array();// ...

  9. win下写任务提交给集群

    一,复制和删除hdfs中的文件 import org.apache.hadoop.fs.{FileSystem, Path} import org.apache.spark.{SparkConf, S ...

  10. 火车票抢票API 根据乘客的车次与座席要求快速订票出票

    火车票抢票API 根据乘客的车次与座席要求快速订票出票:https://www.juhe.cn/docs/api/id/257 1.站站查询 接口地址:http://v.juhe.cn/grabTic ...