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

题意:一个数组有n个数,每个数初始值为0。对该数组进行m次操作,每一次操作将数组中的a[i]=a[i]+x+d*abs(i-j),求m次操作后该数组所有元素和的最大的平均值是多少(看了好久的题,最后看了猛神的博客才把题意弄懂(;´д`)ゞ)。

实现:

          因为数组的输出值全是0,所以用ans代表当前数组的元素和(ans初始值为0)。因为每次操作,每个元素都要先加上x,

          所以ans=ans+x*n

          接下来求该操作中的d*abs(i-j),因为i是一直变化的,每次操作是j是在随意的位置,但是一旦确定就不改变,所以可以看做是求数轴上1~n每个数到某一定点的距离。

          因为d的正负不确定,还要使d*∑abs(i-j)的值最大,所以要对j的位置进行讨论:

          如果d是正数,距离要尽可能大,所以j应该放在端点处;如果是负数,要使距离尽可能小,即需要放在中间位置

PS:最后注意精度问题,好像double会卡39组数据,int第五组都过不去,要用long long,最后再将long long转换成浮点型

AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e6+10;
using namespace std;
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	ll n,m;
	ll x,d;
	cin>>n>>m;
	ll ans=0;
	while(m--)
	{
		cin>>x>>d;
		ans+=n*x;//求数组里元素的总和
		if(d>0)//距离应该最大,j取到两端点处,距离和为:(0+(n-1)*n)/2=n*(n-1)/2
		{
			ans+=d*(n*(n-1)/2);
		}
		else//此时j应在中间,分元素个数为奇数,偶数两种情况
		{
			if(n%2)//如果是奇数,放在正中间
			{
				 ll mid=(n+1)/2;
				 ll x=mid*(mid-1);
				 ans+=d*x;
			}
			else//偶数的话中间两个位置都可以
			{
				ll mid=n/2;
				ll x=(1+mid)*mid/2;
				ans+=d*(2*x-mid);
			}
		}
	}
	printf("%.15lf\n",ans*1.0/n);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值