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. IO 流(InputStream,OutputStream)

    1. InputStream,OutputStream都是抽象类,所以不能创建对象. 1个中文占两个字节 package com.ic.demo01; import java.io.File; imp ...

  2. Jquery radio选中

    radio选中$("input[name=test][value=34]").attr("checked",true);//value=34的radio被选中$ ...

  3. mysql-root本地无法登录处理

    主要有以下几种情况: 1.忘记密码 2.丢失root对localhost的访问权限或者对应的host授权 解决方案: ----------------------------------------- ...

  4. MythXinWCF通用宿主绿色版发布(一键启动,方便快捷)

    这是我开发的一款免费的,绿色的WCF宿主工具,一键启动,方便快捷,省去了安装和配置IIS的麻烦,也不需要配置文件. 该工具运行环境为.Net Framework 4.5.1(这个是必装的,win10自 ...

  5. WebLogic Server 12.1.2后的字符型安装模式

    weblogic Server 12.1.1全部都可以用原来方式. WebLogic Server 12.1.2后已经取消了console安装模式,目前只有gui和静默安装模式.并且安装方式下也有很大 ...

  6. javascript - 闭包之一些常见的写法

    /** * 防御型分号 */ ; (function () { //code.... console.log('一开始,我自己执行了!!!'); })(); /** * 形参 -> 实参 * 定 ...

  7. 自己定义modal动画

    在非常多场景中.我们都须要实现各种动画.这回我们来尝试搞一下控制器间跳转的modal动画. - (void)touchesBegan:(NSSet<UITouch *> *)touches ...

  8. mac 上多版本python 共存

    Mac上自带了Python2.x的版本,有时需要使用Python3.x版本做开发,但不能删了Python2.x,可能引起系统不稳定,那么就需要安装多个版本的Python. 1.安装Python3.x版 ...

  9. css样式布局中position的那些事儿

    哎,页面布局及设计开发.对于一个一直从事后台开发来说屌丝来说,确实是件非常费时.费力,非常艰难的一件事. 今晚是想实现把多张重叠在一起.或是标记一张图片中不同的位置然后赋以超链接.花了一晚上的时间,才 ...

  10. Hibernate单向“一对多”关联

    1. 基于连接表的单向“一对多”关联,应该优先被采用其中指定many-to-many的unique="true",为单向“一对多”,不指定就是单向“多对多” <class n ...