重要的是从小到大枚举,每当一个人交钱后,要更新剩下的平均值,这样才能使得标准差最小
#include<bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n;
long double s;
int a[N];
int main()
{
cin >> n >> s;
long double avg;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
avg = s / n;
sort(a, a + n);
double cnt;
long double res=0;
for (int i = 0; i < n; i++) {
cnt = s / (n - i);
if (a[i] < cnt)
cnt = a[i];
res += (cnt - avg) * (cnt - avg);
s -= cnt;
}
printf("%.4Lf\n", sqrt(res / n));
return 0;
}