2017-5-14 湘潭市赛 Partial Sum 给n个数,每次操作选择一个L,一个R,表示区间左右端点,该操作产生的贡献为[L+1,R]的和的绝对值-C。 0<=L<R<=n; 如果选过L,R这两个位置,那么以后选择的L,R都不可以再选择这两个位置。最多操作m次,求可以获得的 最大贡献和。
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次,求可以获得的 最大贡献和。的更多相关文章
- Partial Sum
Partial Sum Accepted : 80 Submit : 353 Time Limit : 3000 MS Memory Limit : 65536 KB Partial Sum ...
- 【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 ...
- 2017.10.14 Java的流程控制语句switch&&随机点名器
今日内容介绍 1.流程控制语句switch 2.数组 3.随机点名器案例 ###01switch语句解构 * A:switch语句解构 * a:switch只能针对某个表达式的值作 ...
- HihoCoder 1629 Graph (2017 ACM-ICPC 北京区域赛 C题,回滚莫队 + 启发式合并 + 可撤销并查集)
题目链接 2017 ACM-ICPC Beijing Regional Contest Problem C 题意 给定一个$n$个点$m$条边的无向图.现在有$q$个询问,每次询问格式为$[l, ...
- 2017南开ACM校赛(网络赛) 民间题解
orz 首先说一下这个只是民间题解,可能会有很多错误 程序还没有评测,所以可能存在问题 C题比赛的时候没想到..后来发现是个模板题,所以没有代码 希望这份题解能对读者有所启发吧... A题 直接倒序枚 ...
- NYOJ 927 The partial sum problem 【DFS】+【剪枝】
The partial sum problem 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 One day,Tom's girlfriend give him a ...
- 部分和(partial sum)在算法求解中的作用
C++ 的 STL 库的 <numeric> 头文件的 partial_sum 函数已实现了对某一序列的 partial sum. partial_sum(first, last, des ...
- 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 ...
- 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 ...
随机推荐
- nhibernate change connection
http://stackoverflow.com/questions/4335827/changing-nhibernate-connectionstring http://stackoverflow ...
- [Java基础] 使用JMAP dump及分析dump文件
转载:http://blog.csdn.net/kevin_luan/article/details/8447896 http://liulinxia02.blog.163.com/blog/stat ...
- 本地DNS如何解析公网域名
DNS服务器图解: 为了服务于公司内部局域网应用,如域.本地网站.论坛.OA.ERP系统等,我们通常搭建本地DNS服务器. 将本地DNS服务器设置为首选DNS,本地系统解析访问是快了. 但内部DNS怎 ...
- 【千纸诗书】—— PHP/MySQL二手书网站后台开发之知识点记录
前言:使用PHP和MySQL开发后台管理系统的过程中,发现有一些通用的[套路小Tip],这里集中记录一下.结合工作中ing的后台业务,我逐渐体会到:除了技术知识外.能使用户体验好的“使用流程设计”积累 ...
- Unity3d 嵌入GoogleMap
原地址“http://cl314413.blog.163.com/blog/static/190507976201442371753142/ 新建工程导入Google Maps for Unity包 ...
- import * as obj from 'xx'
import * as obj from 'xx' 这种写法是把所有的输出包裹到obj对象里 例如: xx里中: export function hello(){ return '我是hello 内 ...
- lodash 检查值是否存在 includes
_.includes(collection, value, [fromIndex=0]) 检查 值 是否在 集合中,如果集合是字符串,那么检查 值 是否在字符串中. 其他情况用 SameValueZe ...
- 【DB2】报错:-30090 25000 指定的操作对远程执行失败
场景描述: 数据库:DB_1,DB_2 现在在DB_1中建立NICKNAME为CST_INFO_NICK,并且该别名指向数据库DB_2的CST_INFO表,在DB_1中建立存储过程,该存储过程需要 ...
- (三)Oracle学习笔记—— sql语句
0. scott 用户默认表介绍 scott用户Tables目录下包含四张表 1. insert(插入)语句 给指定列插入数据: ,'xx'); 插入全部列数据: ,'xx','lll'); 2. u ...
- How to learn a new technology
是什么?为什么会出现? 这一阶段主要是对该技术有一个整体了解,他所解决的是什么问题,他的整体结构等. 怎么做? 最简单的是找一个上手视频,因为视频是非常直观的展示了技术的使用.先学会用是最根本的,对于 ...