zoukankan      html  css  js  c++  java
  • 2017-5-14 湘潭市赛 Partial Sum 给n个数,每次操作选择一个L,一个R,表示区间左右端点,该操作产生的贡献为[L+1,R]的和的绝对值-C。 0<=L<R<=n; 如果选过L,R这两个位置,那么以后选择的L,R都不可以再选择这两个位置。最多操作m次,求可以获得的 最大贡献和。

    Partial Sum
    Accepted : 30           Submit : 119
    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 0≤l<r≤n and add |∑rj=l+1aj|−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 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
    
    
    Source
    XTU OnlineJudge 
    
    /**
    题目:Partial Sum
    链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1264
    题意:给n个数,每次操作选择一个L,一个R,表示区间左右端点,该操作产生的贡献为[L+1,R]的和的绝对值-C。
    0<=L<R<=n; 如果选过L,R这两个位置,那么以后选择的L,R都不可以再选择这两个位置。最多操作m次,求可以获得的
    最大贡献和。
    思路:脑洞。原公式可以转化为|sum[r]-sum[l]|-c,sum[i]表示前i项的前缀和。
    由于绝对值的影响,所以对前缀和排序,然后l从左边开始递增枚举,r从右边开始递减枚举,每次组成一对(sum[l],sum[r])作为贡献计算。
    
    
    */
    
    #include<bits/stdc++.h>
    
    using namespace std;
    typedef long long LL;
    const int maxn = 1e5+100;
    int a[maxn], sum[maxn];
    int main()
    {
        int n, m, c;
        while(scanf("%d%d%d",&n,&m,&c)!=EOF)
        {
            //cout<<"yes"<<endl;
            for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
            sum[0] = 0;
            for(int i = 1; i <= n; i++){
                sum[i] = a[i]+sum[i-1];
            }
            sort(sum,sum+1+n);
            int l = 0, r = n;
            LL ans = 0;
            while(m--){
                int value = abs(sum[r]-sum[l]);
                if(value<=c) break;
                ans += value-c;
                l++, r--;
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
    
     
  • 相关阅读:
    Java一棵树
    Mac常用设置备忘
    全球测速工具
    mac常用软件
    APP https抓包
    spring无法启动常见原因及排查方法
    Java_cpu飙升排查
    charles_https_通过模拟器安装APP然后抓包
    源码探究Java_HashMap
    The server encountered an internal error that prevented it from fulfilling this request.(JsonMappingException: Conflicting getter definitions)
  • 原文地址:https://www.cnblogs.com/xiaochaoqun/p/6856016.html
Copyright © 2011-2022 走看看