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. java对象详解

    java对象及线程详解 内存布局 普通对象布局 数组的内存布局 内部类的内存布局 对象分解 对象头-mark word(8字节) 实例数据 对齐填充(可选) java锁分析 volatile关键字 v ...

  2. Linux Hook 笔记

    相信很多人对"Hook"都不会陌生,其中文翻译为"钩子".在编程中, 钩子表示一个可以允许编程者插入自定义程序的地方,通常是打包好的程序中提供的接口. 比如,我 ...

  3. 电脑(台式机||笔记本)开机password忘记通用解决方法

    方法:直接制作一个老毛桃装机版u盘启动盘 网址:老毛桃官网 步骤:依照网址的解说,将制作好的U盘插入到电脑的usb插口.执行Windows 登入password破解菜单,搜索password所在的盘符 ...

  4. centos selinux学习记录

    SELinux 全称 Security Enhanced Linux (安全强化 Linux),是 MAC (Mandatory Access Control,强制访问控制系统)的一个实现,目的在于明 ...

  5. Java平时需要注意的事项

    1.String 相等 稍微有点经验的程序员都会用equals比较而不是用 ==,但用equals就真的安全了吗,看下面的代码 user.getName().equals("xiaoming ...

  6. JAVA学习笔记 -- 多线程之共享资源

    在多线程程序执行过程中,可能会涉及到两个或者多个线程试图同一时候訪问同一个资源.为了防止这样的情况的发生,必须在线程使用共享资源时给资源"上锁",以阻挡其他线程的訪问. 而这样的机 ...

  7. JAVA利用HttpClient进行POST请求(HTTPS)

    目前,要为另一个项目提供接口,接口是用HTTP URL实现的,最初的想法是另一个项目用jQuery post进行请求. 但是,很可能另一个项目是部署在别的机器上,那么就存在跨域问题,而JQuery的p ...

  8. select * from (select user())a 为什么是查询user()的意思?

    步骤:1.先查询 select user() 这里面的语句,将这里面查询出来的数据作为一个结果集 取名为 a2.然后 再 select * from a 查询a ,将 结果集a 全部查询出来

  9. Loadrunner11之VuGen常用函数lr_user_data_point(一)

    Loadrunner11之VuGen常用函数lr_user_data_point(一) 上一篇 / 下一篇  2011-11-15 00:15:33 / 精华(1) / 置顶(1) / 个人分类:性能 ...

  10. python challenge - orc.py

    http://www.pythonchallenge.com/pc/def/ocr.html recognize the characters. maybe they are in the book, ...