湘潭OJ 1264 挺好的一道贪心题
2017-05-25 21:33
786 查看
题目链接
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 mostm
times.
If each end can be selected at most once (either as left or right), find out the maximum sum Bobo may have.
The first line contains three integers
n,m,C.The
second line contains
n
integers a1,a2,…,an.
2≤n≤105
1≤2m≤n+1
|ai|,C≤104
The sum of n
does not exceed 106.
选m个区间,让m个(区间和-c)的和最大。可以不选。(相当于选区间(l,r),l==r,什么都不会加)。
考虑贪心,,因为是区间和绝对值,所以可以求出所有前缀和,然后对前缀和排序,这时候只要从l=0,r=n。开始枚举区间就好了。
例如 7 8 9 -1 -2 -3 -4。选0-3,1-4,5-7;
Partial Sum
Bobo has a integer sequencea1,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 mostm
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.
2≤n≤105
1≤2m≤n+1
|ai|,C≤104
The sum of n
does not exceed 106.
Output
For each test cases, output an integer which denotes the maximum.Sample Input
4 1 1 -1 2 2 -1 4 2 1 -1 2 2 -1 4 2 2 -1 2 2 -1 4 2 10 -1 2 2 -1
Sample Output
3 4 2 0
选m个区间,让m个(区间和-c)的和最大。可以不选。(相当于选区间(l,r),l==r,什么都不会加)。
考虑贪心,,因为是区间和绝对值,所以可以求出所有前缀和,然后对前缀和排序,这时候只要从l=0,r=n。开始枚举区间就好了。
例如 7 8 9 -1 -2 -3 -4。选0-3,1-4,5-7;
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #include<string> using namespace std; typedef long long ll; typedef pair<int,int>P; const int INF=0x3f3f3f3f; ll sum[100010]; int main() { int n,m,c; while(scanf("%d%d%d",&n,&m,&c)!=EOF) { sum[0]=0; for(int i=1;i<=n;i++) { int x;scanf("%d",&x); sum[i]=sum[i-1]+x; } sort(sum,sum+n+1); int l=0,r=n; ll ans=0;; while(m--) { int v=sum[r]-sum[l]; if(v<=c) break; ans+=v-c;l++,r--; } printf("%I64d\n",ans); } return 0; }
相关文章推荐
- 2017-3-17 vijos1844一道坑爹的贪心
- 阶乘之和(南阳oj,贪心,91)
- 【南阳理工oj】喷水装置(一)(贪心)
- 湘潭oj_1252 Defense Tower
- 湘潭邀请赛G- Parentheses(贪心,思维)
- 【杭电oj】2111 - Saving HDU(贪心,水)
- 【HDU-oj]-1009-FatMouse' Trade(贪心)
- 一道贪心的题,我想知道这种方法为什么不对。。。
- 【贪心】【POJ-2231&&AOJ-231】Moo Volume
- 湘潭OJ 1305 成对的字符串
- 【南阳OJ 6】 喷水装置 一(贪心)
- NYOJ1036 非洲小孩(又一道贪心水题,区间选点问题)
- 湘潭oj_1250 Super Fast Fourier Transform
- 【杭电oj】5248 - 序列变换(贪心 & 二分)
- 豪杰信息杯I 湘潭oj1268-Strange Optimization
- 过河问题(南阳oj47)(贪心)
- 【HDU-oj】-2124-Repair the Wall(贪心)
- [福建师大附中OJ 1264]烽火台
- 哈理工oj 1079 I can do it【贪心】
- AOJ-AHU-OJ-517 序列(贪心)