cf Educational Codeforces Round 47 C. Annoying Present

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tengfei461807914/article/details/82190925

原题:
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的整数序列 a i ,然后给你m个x和d,让你执行这样的操作,任意找一个值j,j∈[1,n-1],对每个 a i 执行 a i + x k + d k · | i j | 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;
}

思路:

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

s 1 = i = 1 n ( x 1 + d 1 · | i j | )

s 2 = i = 1 n ( x 2 + d 2 · | i j | )

s m = i = 1 n ( x m + d m · | i j | )
现在就是求 a n s = ( s 1 + s 2 + . . . s m ) / n 最大

可以将公式 s k = i = 1 n ( x k + d k · | i j | ) 整理一下,有

s k = i = 1 n x k + d k · i = 1 n ( | i j | )

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

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

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

最后累加求平均数即可

猜你喜欢

转载自blog.csdn.net/tengfei461807914/article/details/82190925