多校 1001 Acperience

来源:互联网 发布:欧佩克石油库存数据 编辑:程序博客网 时间:2024/05/16 06:27
  • 题意:给一组数W,-10000<=wi<=10000,( wi-@bi)^2,其中bi为-1或1,@>=0。求  各个 ( wi-@bi)^2  和的最小值
  • 解题思路,求这一列数的方差,@取其平均数,当wi<0时,wi=-wi;因为bi可以为-1,相减总比相加要小。
  • 要注意,在求方差时不能直接求,中间结果会暴long long,要用S^2=1/n * [ (x1^2+x2^2+x3^2+……)- n*x^2]
    α 
    #include <iostream>#include <stdio.h>#include <algorithm>#include <cmath>#include <map>#include<string.h>#define ll long long#define ull unsigned long longusing namespace std;const int maxn=100010;ll gcd(ll a,ll b){    return b==0?a:gcd(b,a%b);}ll a[maxn];int main(){    int T;    scanf("%d",&T);    long long  n;    while(T--)    {        scanf("%I64d",&n);        ll ave=0;        ll sum=0;        for(int i=0;i<n;i++)        {            long long x;            scanf("%I64d",&x);            if(x<0)            {                x=-x;            }            ave+=x;            sum+=x*x;        }        ave=ave*ave;        ll g=gcd(n,ave);        ave/=g;        n/=g;        printf("%I64d/%I64d\n",sum*n-ave,n);    }    return 0;}

    α α α α αα
0 0