XTU 1264 Partial Sum【思维+贪心】
来源:互联网 发布:有深度的书籍推荐知乎 编辑:程序博客网 时间:2024/06/06 11:04
Partial Sum
Accepted : 75 Submit : 326Time Limit : 3000 MS Memory Limit : 65536 KBPartial Sum
Bobo has a integer sequence
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
2≤n≤105 1≤2m≤n+1 |ai|,C≤104 - The sum of
n does not exceed106 .
Output
For each test cases, output an integer which denotes the maximum.
Sample Input
4 1 1-1 2 2 -14 2 1-1 2 2 -14 2 2-1 2 2 -14 2 10-1 2 2 -1
Sample Output
3420
Source
XTU OnlineJudge题目大意:
给你N个数,最多有m次操作,每次操作选择两个端点,l,r,取得的值为|Σ(【l+1,r】)a【i】|-C.
任意点最多只能做为一次端点。
问最多能够取得的值为多少?
思路:
每次操作可以看成两个区间和相减:
|Σ(【l+1,r】)a【i】|=Sum【1,r】-Sum【1,l】或者是-(Sum【1,r】-Sum【1,l】);
那么每次操作肯定是要加一个前缀和,以及减去一个前缀和。
那么m次操作就是加上m个前缀和,以及减去m个前缀和。
那么我们贪心的去操作,每次操作的时候都加入此时没选过的最大的前缀和作为正的,最小的前缀和作为负的。
那么枚举操作次数然后维护一个最大值即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespace std;#define ll __int64ll a[150000];ll sum[150000];int main(){ int n,m; ll C; while(~scanf("%d%d%I64d",&n,&m,&C)) { for(int i=1;i<=n;i++)scanf("%I64d",&a[i]); for(int i=1;i<=n;i++) { if(i==1)sum[i]=a[i]; sum[i]=sum[i-1]+a[i]; } ll output=0; sort(sum,sum+1+n); ll cnt=0; ll ans=0; for(int i=0;i<m;i++) { cnt+=(sum[n-i]-sum[i]-C); ans=max(ans,cnt); } printf("%I64d\n",ans); }}
阅读全文
0 0
- XTU 1264 Partial Sum【思维+贪心】
- XTU 1264 Partial Sum 【思维 + 前缀和排序】
- xtu 1264 Partial Sum 2017湘潭邀请赛E
- XTU 1264 Partial Sum 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛(湖南)
- XTU 1264 Partial Sum 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛(湖南)
- Partial Sum
- NYOJ927The partial sum problem
- The partial sum problem
- The partial sum problem
- The partial sum problem
- The partial sum problem
- NOOJ927The partial sum problem
- NYOJThe partial sum problem927
- The partial sum problem
- The partial sum problem
- Partial Sum (2017湘潭)
- The partial sum problem
- The partial sum problem
- [从面试题看问题]线程篇(一)
- 互联网产品经理成长记
- iOS开发——性能优化
- tomcat目录结构
- 在二叉查找树中插入节点
- XTU 1264 Partial Sum【思维+贪心】
- Java面向对象
- HDU 2818 Building Block (带权并查集)
- Servlet的生命周期与浏览器访问Servlet
- 95. Unique Binary Search Trees II
- 微服务,微架构[五]之springboot读取properties文件
- View的测量
- |算法讨论|树链剖分 学习笔记
- hacking exposed 作业