列表

详情


NC52864. Partial Sum

描述

Bobo has a integer sequence of length n.
Each time, he selects two ends and add 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.

输入描述

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 .
*
*
*
* The sum of n does not exceed .

输出描述

For each test cases, output an integer which denotes the maximum.

示例1

输入:

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

输出:

3
4
2
0

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(g++ 7.5.0) 解法, 执行用时: 237ms, 内存消耗: 784K, 提交时间: 2022-08-22 19:52:55

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n,m,a[N],sum[N],c;
int main(){
    while(~scanf("%d%d%d",&n,&m,&c)){
        for(int i=1;i<=n;i++) scanf("%d",&a[i]),a[i]+=a[i-1];
        sort(a,a+n+1);
        long long ans = 0;
        for(int i=1;i<=m;i++){
            if(a[n-i+1] - a[i-1] < c) break;
            else ans += a[n-i+1]-a[i-1] - c;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

C++14(g++5.4) 解法, 执行用时: 359ms, 内存消耗: 1144K, 提交时间: 2019-10-04 15:50:47

#include<bits/stdc++.h>
using namespace std;
int i,i0,n,m,T,ans,c,x;
long long pre[100005];
int main() {
    while(~scanf("%d %d %d",&n,&m,&c)) {
        pre[0]=0;
        for(i=1; i<=n; i++)scanf("%d",&x),pre[i]=pre[i-1]+x;
        sort(pre,pre+1+n);
        long long ans=0;
        for(i=0; i<m; i++)ans+=max(0ll,pre[n-i]-pre[i]-c);
        printf("%lld\n",ans);
    }
    return 0;
}

C++ 解法, 执行用时: 491ms, 内存消耗: 1172K, 提交时间: 2022-07-25 20:02:21

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ll a[100005];
    int n, m, c;
    while(cin >> n >> m >> c)
    {
        ll num = 0;
        for (int i = 1; i <= n; i ++ ) cin >> a[i], a[i] += a[i - 1];
        sort(a, a + n + 1);
        for (int i = 0; i < m; i ++ ) num += max(0LL, abs(a[n - i] - a[i]) - c);
        cout << num << endl;
    }
}

上一题