Partial Sum
Partial Sum |
||
| Accepted : 80 | Submit : 353 | |
| Time Limit : 3000 MS | Memory Limit : 65536 KB | |
Partial SumBobo has a integer sequence a1,a2,…,an of length n . Each time, he selects two ends 0≤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. InputThe 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 .
OutputFor each test cases, output an integer which denotes the maximum. Sample Input4 1 1 Sample Output3 |
//题意,最多选 m 次区间的和的绝对值 - c 的值,要求和最大,且所有点最多选一次作为端点
//贪心题,只要把前缀和排序,每次选最大的,最小的,直到选出值为负数,或等于m次,即停止
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define MX 100005
#define LL long long int n,m,c;
int num[MX];
LL sum[MX]; int main()
{
while(~scanf("%d%d%d",&n,&m,&c))
{
sum[]=;
for (int i=;i<=n;i++)
{
scanf("%d",&num[i]);
sum[i]=sum[i-]+num[i];
}
sort(sum,sum++n);
int l = ,r = n;
LL ans = ; while (l<m&&sum[r]-sum[l]>c)
{
ans+=abs(sum[r]-sum[l])-c;
l++,r--;
}
printf("%I64d\n",ans);
}
return ;
}
Partial Sum的更多相关文章
- 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 ...
- 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 seq ...
- 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 ...
- The partial sum problem
算法:搜索 描述 One day,Tom's girlfriend give him an array A which contains N integers and asked him:Can yo ...
- nyoj 927 The partial sum problem(dfs)
描述 One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choo ...
- NYoj The partial sum problem(简单深搜+优化)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=927 代码: #include <stdio.h> #include & ...
- XTU 1264 - Partial Sum - [2017湘潭邀请赛E题(江苏省赛)]
2017江苏省赛的E题,当时在场上看错了题目没做出来,现在补一下…… 题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id ...
随机推荐
- leetcode 二分查找 Search in Rotated Sorted Array
Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose ...
- C++ 字符串分割函数 str_split
void str_split(const std::string & src, const std::string & sep, std::vector<std::string& ...
- 病毒木马查杀实战第022篇:txt病毒研究
前言 反病毒爱好者们非常喜欢讨论的一个问题就是,现在什么样的病毒才算得上是主流,或者说什么样的病毒才是厉害的病毒呢?我们之前的课程所解说的都是Ring3层的病毒.所以有些朋友可能会觉得.那么Ring0 ...
- Django——模板系统过滤器
过滤器,变量的显示形式的改变 一.形式:小写 {{ name | lower }} 二.串联:先转义文本到HTML,再转换每行到 <p> 标签 {{ my_text|escape|line ...
- detach与remove区别,以及detach保留被删除的元素数据,使用
detach() 会保留所有绑定的事件.附加的数据,这一点与 remove() 不同. remove掉元素后,元素再也找不回了.但是detach还能找回来,还能保留下来. 实现方式如下代码: < ...
- jQuery unbind() 方法
jQuery 中的 unbind() 方法是 bind() 方法的反向操作,从每一个匹配的元素中删除绑定的事件. 语法结构: unbind([type][, data]); type是事件类型,dat ...
- python重置变量 sys.defaultencoding
<span style="font-family: Arial, Helvetica, sans-serif;">比如重置变量defaultencoding为utf-8 ...
- Android编程之Fragment使用动画造成Unknown animation name: objectAnimator异常
在为Fragment做切换动画.启动后遇到了一个异常: Caused by: java.lang.RuntimeException: Unknown animation name: objectAni ...
- linux proc目录和常用操作
------------------------------------------------/proc----------------------------------------------- ...
- JAVA代码之RocketMQ生产和消费数据
一.启动RocketMQ [root@master ~]# cat /etc/hosts # Do not remove the following line, or various programs ...