hdu4355

三分;

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;

const double eps=1e-5;
const int MAX=50010;

double p[MAX],w[MAX];
int n;

double equ(double x)
{
    double ans=0;
    for(int i=0;i<n;i++)
    {
        double S=fabs(p[i]-x);
        ans+=w[i]*S*S*S;
    }
    return ans;
}

double ternarysearch(double l,double r)
{
    while(r-l>eps)
    {
        double L=(2*l+r)/3;
        double R=(l+2*r)/3;
        double ans1=equ(L);
        double ans2=equ(R);
        if(ans1>ans2)
            l=L;
        else
            r=R;
    }
    return l;
}

int main()
{
    int T,i;
    scanf("%d",&T);
    for(int t=1;t<=T;t++)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
        scanf("%lf%lf",&p[i],&w[i]);
        double l=p[0],r=p[n-1];
        double tmp=ternarysearch(l,r);
        printf("Case #%d: %.0lf\n",t,equ(tmp));
    }
    return 0;
}
    原文作者:B树
    原文地址: https://blog.csdn.net/aonaigayiximasi/article/details/46844801
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞