【codeforces 1009C】Annoying Present(思维)

C. Annoying Present
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 xx and dd, he chooses an arbitrary position ii (1in1≤i≤n) and for every j[1,n]j∈[1,n] adds x+ddist(i,j)x+d⋅dist(i,j) to the value of the jj-th cell. dist(i,j)dist(i,j) is the distance between positions ii and jj (i.e. dist(i,j)=|ij|dist(i,j)=|i−j|, where |x||x| is an absolute value of xx).

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

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 nn and mm (1n,m1051≤n,m≤105) — the number of elements of the array and the number of changes.

Each of the next mm lines contains two integers xixi and didi (103xi,di103−103≤xi,di≤103) — the parameters for the ii-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 10610−6.

Examples
input
Copy
2 3
-1 3
0 0
-1 -4
output
Copy
-2.500000000000000
input
Copy
3 2
0 2
5 0
output
Copy
7.000000000000000

比赛的时候没写出来,一直WA5,第二天找到了bug结果WA39,后来发现是精度问题,把double改成long long成功AC。

其实这道题表面上看着很复杂,其实只需要讨论几种情况即可。

题意:原本有一个n个0的数组a[],你对它进行m次操作,每次操作让a[j]+=x+d*(dish(i,j))(dish(i,j)代表abs(i-j))。其中i是任意的。让你求经过这m次操作所能得到数组的平均值最大为多少。其实这里有一点贪心的思想,就是我们要尽量的让a[j]最大,那么和x是没有关系的,初始时数组和为ans=0;那么以后每次操作ans肯定会+=n*x;重点是对d的讨论,d<0是我们要让距离和最小,d>=0让距离和最大,这样就达到了我们的步步最优的条件。接下来就很简单了,直接讨论距离和即可(看代码吧)。

#include<bits/stdc++.h>
using namespace std;
#define MAXN 100050
typedef long long ll;
long long x,d;
int main()
{
	ll n;
	ll m;
	ll ans=0;
	cin>>n>>m;
	while(m--)
	{
		cin>>x>>d;
		ans+=n*x; 
		if(d<=0)
		{
			if(n&1)
			{
				ll t=n/2;
				ans+=d*((t)*(t+1));
			}
			else
			{
				ll t=(n-1)/2;
				ans+=d*((t)*(t+1)+n/2);
			}
		}
		else
		{
			ans+=d*((n)*(n-1)/2);
		}
	}
	printf("%.15lf\n",ans*1.0/n);
}
阅读更多

没有更多推荐了,返回首页