2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛 Partial Sum

Partial Sum

Accepted : 124   Submit : 450
Time Limit : 3000 MS   Memory Limit : 65536 KB 

 

Partial Sum

Bobo has a integer sequence a1,a2,,an of length n . Each time, he selects two ends 0l<rn and add |rj=l+1aj|C into a counter which is zero initially. He repeats the selection for at most mtimes.

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 .

  • 2n105
  • 12mn+1
  • |ai|,C104
  • 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

题意:选取一段区间求和取绝对值,加在初始化为0的数值上,选了的区间不能再选,问最大的和是多少

解法:前缀和排序,最大和最小相减加起来就好了

复制代码
 1 #include<bits/stdc++.h>
 2 #define  INF 1000000000
 3 #define ll long long
 4 using namespace std;
 5 ll x[123456];
 6 ll sum;
 7 int main()
 8 {
 9      std::ios::sync_with_stdio(false);
10     ll n,m,a,b;
11     while(cin>>n>>m>>a)
12     {
13         sum=0;
14         ll ans[123456];
15         ans[0]=0;
16         for(int i=1;i<=n;i++)
17         {
18             ll num;
19             cin>>num;
20             ans[i]=ans[i-1]+num;
21         }
22         ll cnt=0;
23         ll Max=0;
24         Max=max(Max,sum);
25         sort(ans,ans+1+n);
26         while(m--)
27         {
28             ll Pmax=ans[n--];
29             ll Pmin=ans[cnt++];
30            // cout<<Pmax<<" "<<Pmin<<endl;
31             sum+=abs(Pmax-Pmin)-a;
32             if(abs(Pmax-Pmin)-a<0) break;
33             Max=max(Max,sum);
34         }
35         cout<<Max<<endl;
36     }
37     return 0;
38 }
复制代码

 

posted @   樱花落舞  阅读(349)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示