Problem Description
Alice got an array of length nn 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 mm 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 ii 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≤105) — the number of elements of the array and the number of changes.
Each of the next m lines contains two integers xi and di (−103≤xi,di≤103) — 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.
Examples
Input
2 3
-1 3
0 0
-1 -4Output
-2.500000000000000
Input
3 2
0 2
5 0Output
7.000000000000000
题意:给一个长度为 n 的数组, 进行 m 次操作,每次操作为:给出 x、d 两个数,任意挑选一个 i (1~d),每个数字加上 x+|i-j|*d,求 m 次操作后最大平均值
思路:对于 x,每次数组的和 sum 一定会增加 n*x,而 (Σ|i-j|)*d 与我们选的 i 有关系,如果 d>0,就让 Σ|i-j| 尽量大,如果d<0,我们就让 Σ|i-j| 尽量小,而 Σ|i-j| 的最大值就是 i 取 0 或 n 的时候,Σ|i-j| 的最小值就是 i 取 n/2 的时候,因此维护 sum 即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 10001
#define MOD 1e9+7
#define E 1e-6
#define LL long long
using namespace std;
int main()
{
LL n,m;
cin>>n>>m;
LL sum=0;
LL maxx=n*(n-1)/2,minn=(n/2+1)*(n/2)-(n%2?0:n/2);
while(m--)
{
LL x,d;
cin>>x>>d;
sum+=x*n;
if(d>0)
sum+=maxx*d;
if(d<0)
sum+=minn*d;
}
printf("%.15lf",(double)sum/n);
return 0;
}