1. 程式人生 > 實用技巧 >ZJU-ICPC Summer 2020 Contest 8 C

ZJU-ICPC Summer 2020 Contest 8 C

#include<bits/stdc++.h>
using namespace std;

int n,m;
int cnt;

int a[450000];
long double tot1,tot2;
long double s1[450000],s2[450000];

int main(){
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= n; ++ i)
    for(int j = 1; j <= m; ++ j){
        int x; scanf("%d",&x);
        a[++ cnt] = x; tot2 += x;
    }
    sort(a + 1, a + cnt + 1);
    
    for(int i = 1; i <= cnt; ++ i){
        s1[i] = a[i];
        s2[i] = 1ll * a[i] * a[i];
        s1[i] += s1[i - 1];
        s2[i] += s2[i - 1];
    }
    
    long double ans = 1e30;
    
    tot1 = 0;
    for(int i = 0; i <= cnt; ++ i){
        tot1 += a[i]; tot2 -= a[i];
        if(a[i] == a[i + 1]) continue;
        long double t1 = 0, t2 = 0;
        
        if(tot1 != 0){
            t1 += s2[i];
            t1 += (tot1 * tot1 - 2 * s1[i] * tot1) / i;
        }
        if(tot2 != 0){
            t2 += s2[cnt] - s2[i];
         	t2 += (tot2 * tot2 - 2 * (s1[cnt] - s1[i]) * tot2 )/ (cnt - i);
        }
        
        //printf("%Lf\n",t1 + t2);
        if(ans > t1 + t2) ans = t1 + t2;
    }
    printf("%.20Lf\n",ans / cnt);
    return 0;
}