zoukankan      html  css  js  c++  java
  • 2017ccpc全国邀请赛(湖南湘潭) E. Partial Sum

    E. Partial Sum
    Bobo has a integer sequence a 1 ,a 2 ,...,a n of length n. Each time, he selects two ends 0 ≤ l < r ≤ n and
    add |
    ∑ r
    j=l+1 a j | − 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.
    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 a 1 ,a 2 ,...,a n .
    • 2 ≤ n ≤ 10 5
    • 1 ≤ 2m ≤ n + 1
    • |a i |,C ≤ 10 4
    • The sum of n does not exceed 10 6 .
    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

    官方题解:

    前缀和排下序,最大前缀和-最小前缀和就是最大区间和,一直枚举就行了

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<iostream>
    #include<queue>
    #include<map>
    #include<cmath>
    #include<set>
    #include<stack>
    #define ll long long
    #define max(x,y) (x)>(y)?(x):(y)
    #define min(x,y) (x)>(y)?(y):(x)
    #define cls(name,x) memset(name,x,sizeof(name))
    using namespace std;
    const int inf=1<<28;
    const int maxn=100010;
    const int maxm=110;
    const int mod=1e9+7;
    const double pi=acos(-1.0);
    int n,m,c;
    int num[maxn],sum[maxn];
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(~scanf("%d%d%d",&n,&m,&c))
        {
            sum[0]=0;
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&num[i]);
                sum[i]=sum[i-1]+num[i];
            }
            sort(sum,sum+n+1);
            ll ans=0;
            int l=0,r=n;
            for(int i=0;i<m&&l<r;i++)
            {
                if(sum[r]-sum[l]-c>=0)
                {
                    ans+=sum[r]-sum[l]-c;
                    r--,l++;
                }
            }
            printf("%I64d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    iOS提醒用户进入设置界面进行重新授权通知定位等功能
    JQ获得ul li 值导成字符串
    “真的”
    原来还有插入代码功能 我还挨个缩进呢 好白菜啊
    ffmpeg 批处理
    DW english
    CI 分页“第一页”问题
    Discuz论坛发帖统计
    我的产品被评为一个“玩具”
    根据css文件采集图片
  • 原文地址:https://www.cnblogs.com/mgz-/p/6857933.html
Copyright © 2011-2022 走看看