0分,但看不出写得有什么问题,求助大佬相助
查看原帖
0分,但看不出写得有什么问题,求助大佬相助
745926
yu7uan楼主2024/2/25 17:27

用了方差公式变形

#include<bits/stdc++.h>
#define rep(i, a, b) for(int i = (a); i <= (b); i ++)
// #define int long long
#define sqr(x) ((x)*(x))
using namespace std;

const int M = 5e5 + 5;
long double a[M];

signed main(){
	
	int n;
	long double S;
	cin >> n >> S;
	rep(i, 1, n) cin >> a[i];
	
	sort(a + 1, a + 1 + n);
	
	long double res = 0, sum = 0;
	//用了方差公式变形 
	rep(i, 1, n){
		int t = sum + (n - i + 1) * a[i];
		if(t > S){
			long double tmp = (S - sum) / (n - i + 1);
			res += sqr(tmp) * (n - i + 1);
//			cout << i << ' ' << tmp << endl;
			break;
		}
		sum += a[i];
		res += sqr(a[i]);
	}
//	cout << res << ' ' << sum << endl;
	long double ans = res / n - sqr(S / n);
	if(abs(ans) < 1e-10) ans = 0;
	else ans = sqrt(ans);
	printf("%.4Lf", ans);
	return 0;
}

2024/2/25 17:27
加载中...