题意
给你一个长度为n的数组,初始都为0,然后每次给你一个x和d,然后让你随意挑一个位置pos,然后让 a[i] = a[i] + x + d *abs(pos-i),之后问你m次操作后可以得到的最大平均数是什么
思路
数组数值开始都是0,我们知道x是一定要加的所以无法考虑得到最大值,最小值,所以就是选的位置pos,那么很明显我要我操作之后的总和最大,之后得到的平均数才最大,那么就看d和pos了,我们知道 选择最左边或者最右边的时候abs(pos-i)有最大值,选中间的时候有最小值那么的话就有,当d小于0的时候我们取中间,d大于0的时候我们取两边就行了
代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
long long sum1 = 0 , sum2 = 0;
scanf("%d%d",&n,&m);
for(int i = 1 ; i <= n ; i++)
{
sum1 += (i-1);
if(!n%2) sum2 += abs((n/2) - i);
else sum2 += (abs((n/2) + 1 - i));
}
long long ans = 0 ;
int x,d;
for(int i = 0 ; i < m ; i++)
{
scanf("%d%d",&x,&d);
if(d < 0)
{
ans += n * x + d * sum2;
}
else ans += n * x + d * sum1;
}
double ANS = double(ans) / n;
printf("%.15f\n",ANS);
}