cf Educational Codeforces Round 47 C. Annoying Present

原题:
Alice got an array of length n as a birthday present once again! This is the third year in a row!
And what is more disappointing, it is overwhelmengly boring, filled entirely with zeros. Bob decided to apply some changes to the array to cheer up Alice.

Bob has chosen m changes of the following form. For some integer numbers x and d,he chooses an arbitrary position i (1≤i≤n) and for every j∈[1,n] adds x+d⋅dist(i,j) to the value of the j-th cell. dist(i,j) is the distance between positions i and j (i.e. dist(i,j)=|i−j|, where |x| is an absolute value of x).

For example, if Alice currently has an array [2,1,2,2] and Bob chooses position 3 for x=−1 and d=2 then the array will become [2−1+2⋅2, 1−1+2⋅1, 2−1+2⋅0, 2−1+2⋅1] = [5,2,1,3]. Note that Bob can’t choose position i outside of the array (that is, smaller than 1 or greater than n).

Alice will be the happiest when the elements of the array are as big as possible. Bob claimed that the arithmetic mean value of the elements will work fine as a metric.

What is the maximum arithmetic mean value Bob can achieve?

Input
The first line contains two integers n and m(1≤n,m≤10^5) — the number of elements of the array and the number of changes.
Each of the next m lines contains two integers xi and di (−10^3≤xi,di≤10^3) — the parameters for the i-th change.

Output
Print the maximal average arithmetic mean of the elements Bob can achieve.

Your answer is considered correct if its absolute or relative error doesn’t exceed 10^-6.

中文:

给你一个长度为n的整数序列 ai a i ,然后给你m个x和d,让你执行这样的操作,任意找一个值j,j∈[1,n-1],对每个 ai a i 执行 ai+xk+dk|ij| a i + x k + d k · | i − j | i[1,n],k[1,m] i ∈ [ 1 , n ] , k ∈ [ 1 , m ] 。最后问你执行完这m个这样的操作后,总和的平均值最大是多少?

#include<bits/stdc++.h>
using namespace std;


typedef long long ll;
const int maxn=100001;

ll x[maxn],d[maxn];
ll a[maxn];
ll n,m;

int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        ll maxd=0,mind=0;
        ll ans=0;
        memset(a,0,sizeof(a));
        for(int i=1;i<=m;i++)
        {
            cin>>x[i]>>d[i];
            ans+=x[i]*n;
        }

        if(n%2)//奇数个数
        {
            ll tmp=n/2;
            mind=tmp*(tmp+1);
            maxd=(n-1)/2*n;
        }
        else//偶数个数
        {
            mind=n/2*n/2;
            maxd=n/2*(n-1);
        }
        for(int i=1;i<=m;i++)
        {
            if(d[i]<0)
                ans+=mind*d[i];
            else
                ans+=maxd*d[i];
        }
        double dans=(double)ans;

        cout<<fixed<<setprecision(7)<<dans/(n*1.0)<<endl;
    }

    return 0;
}

思路:

根据题意描述,可以写成下面公式

s1=ni=1(x1+d1|ij|) s 1 = ∑ i = 1 n ( x 1 + d 1 · | i − j | )

s2=ni=1(x2+d2|ij|) s 2 = ∑ i = 1 n ( x 2 + d 2 · | i − j | )

sm=ni=1(xm+dm|ij|) s m = ∑ i = 1 n ( x m + d m · | i − j | )
现在就是求 ans=(s1+s2+...sm)/n a n s = ( s 1 + s 2 + . . . s m ) / n 最大

可以将公式 sk=ni=1(xk+dk|ij|) s k = ∑ i = 1 n ( x k + d k · | i − j | ) 整理一下,有

sk=ni=1xk+dkni=1(|ij|) s k = ∑ i = 1 n x k + d k · ∑ i = 1 n ( | i − j | )

可以看到x的值不受到 j j 取值的影响,那么每一项sk的最大或最小就由 |ij| | i − j | 决定

如果 dk d k 小于0,那么让|i-j|取最小值,如果 dk d k 大于0,让 |ij| | i − j | 取最大值即可。

可以判断,当j取1或者n时, ni=1(|ij|) ∑ i = 1 n ( | i − j | ) 得到最大,当j取 n/2 n / 2 时, ni=1(|ij|) ∑ i = 1 n ( | i − j | ) 最小。

最后累加求平均数即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值