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 ...
随机推荐
- NSPredicate 谓词总结 数组过滤 模糊匹配
NSPredicate 用于指定过滤条件,主要用于从集合中分拣出符合条件的对象,也可以用于字符串的正则匹配. NSPredicate常用方法介绍 1.创建NSPredicate(相当于创建一个过滤条件 ...
- NDK之打印调试信息Log
第一步:gradle配置ndk ndk { moduleName "Iso8583Lib" ldLibs "log", "z", " ...
- IOS学习笔记39--NSString各种语法的使用
今天就NSString的各种语法学习学习,以后慢慢补充: 1.字符串的遍历 NSString *string = @"CHENGWULI"; //字符串的长度 int count ...
- SQL Server on Linux: How? Introduction: SQL Server Blog
SQL Server Blog Official News from Microsoft’s Information Platform https://blogs.technet.microsoft. ...
- rem 自适应
最近在写一个关于小说阅读的webApp,由于没有借用任何框架,所以很多底层的内容都需要自己去解决,幸好的是这次只是关于移动端的内容,还不至于去向着jquery的方向码代码.言归正传,前几天在处理底色切 ...
- nativeexcel数据集导出excel
nativeexcel数据集导出excel uses Dataset2Excel; procedure Tfdm.exportXLS(dataset: TDataSet);var xls: TData ...
- iOS开发笔记_5.线程,HTTP请求,定时器
说起线程,不会陌生了,操作系统课程里已经详细介绍了这个东东,这里就不解释了,想要了解的问问百度或者翻翻书. 线程的创建 总结了昨天的学习,有下面几种创建的方式. //第一种 NSThread *t = ...
- iOS 中json解析数据出现中文乱码的问题
一般服务器的编码格式都是UTF8,这样通过json解析下来的的数据,一般中文是不会出现乱码,但是如果服务器的编码格式不是UTF8,通过json解析的数据中的中文容易出现luan乱码,怎么解决这个问题呢 ...
- easyui-validatebox 的简单长度验证
验证: 页面代码: <form id="invoiceEdit"> <input id="fpdm" name="fpdm" ...
- Calendar抽象类返回自己和Integer.TYPE和int.class
public class Calend { public static void main(String[] args) { Calendar cal=Calendar.getInstance();/ ...