标签:数学
4 9 1 1 1 1 1 0 0 1 1 9 1 1 0 0 1 1 1 1 1 4 0 0 1 1 4 0 1 1 1
1.428571 1.000000 0.000000 0.000000
#include<cstdio> const int N = 100010; int sum[N], seg[N]; typedef __int64 LL; bool Check_Biger(int a1, int b1, int a2, int b2) { return (LL)(sum[b1] - sum[a1 - 1]) * (b2 - a2 + 1) >= (LL)(sum[b2] - sum[a2 - 1]) * (b1 - a1 + 1); } int main() { int T, n, i, a; scanf("%d",&T); while(T--) { scanf("%d",&n); sum[0] = 0; //sum[i]表示从A1到Ai一共有多少个1 for(i = 1; i <= n; i++) { scanf("%d",&a); sum[i] = sum[i-1] + a; } int length = 0, id; for(i = 1; i <= n; i++) { id = i; while(length > 0 && Check_Biger(seg[length], id-1, id, i)) { //如果前面一段的平均值不小于后面的平均值,则合并这两段 id = seg[length--]; } seg[++length] = id; } seg[++length] = n + 1; double ans = 0; for(i = 1; i < length; i++) { int l = seg[i]; //当前段的左端点 int r = seg[i+1] - 1; //当前段的右端点 int len = r - l + 1; //当前段的长度 double x = (double(sum[r]) - sum[l-1]) / len; //当前段每一个元素都取这一段的平均值 ans += sum[r] - sum[l-1] + len * x * x - 2 * (sum[r] - sum[l-1]) * x; //(Ai - x)^2 } printf("%lf\n", ans); } return 0; }
hdu 4923 Room and Moor(数学题)2014多校训练第6场,布布扣,bubuko.com
hdu 4923 Room and Moor(数学题)2014多校训练第6场
标签:数学
原文地址:http://blog.csdn.net/lyhvoyage/article/details/38441343