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 x).
For example, if Alice currently has an array 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?
The first line contains two integers 1≤n,m≤105) — the number of elements of the array and the number of changes.
Each of the next i-th change.
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.
2 3
-1 3
0 0
-1 -4
-2.500000000000000
3 2
0 2
5 0
7.000000000000000
这题分析一下题目 就是对d的正负进行分类讨论
d>=0 则选择 i=1; 反之 i=(n+1)/2;
代码量也特别少
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1e5 + 10; 4 const int INF = 0x3fffffff; 5 typedef long long LL; 6 using namespace std; 7 LL n, m, x, d, ans = 0; 8 int main() { 9 scanf("%lld%lld", &n, &m); 10 for (int i = 0 ; i < m ; i++) { 11 scanf("%lld%lld", &x, &d); 12 if (d >= 0) ans += n * x + d * (n - 1) * n / 2; 13 else ans += n * x + d * (n - (n + 1) / 2) * ((n + 1) / 2); 14 } 15 printf("%.8f\n", 1.0 * ans / n); 16 return 0; 17 }