Party All the Time HDU - 4355 三分

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leekerian/article/details/82595613
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>

using namespace std;
const double eps=1e-5;

int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    if(x<0)
        return -1;
    else
        return 1;
}
struct node
{
    double x,w;
}p[50010];

int m;

double f(double x)
{
    double ans=0;
    for(int i=0;i<m;i++)
    {
        double t=fabs(p[i].x-x);
        ans+=t*t*t*p[i].w;
    }
    return ans;
}
double three_search(double l,double r)
{
    while(sgn(r-l)>0)
    {
        double lmid=l+(r-l)/3;
        double rmid=r-(r-l)/3;
        if(f(lmid)<=f(rmid))
            r=rmid;
        else
            l=lmid;
    }
    return f(l);
}
int main()
{
    int n;
    cin>>n;
    int cont=1;
    while(n--)
    {
        cin>>m;
        for(int i=0;i<m;i++)
        {
            //cin>>p[i].x>>p[i].w;
            scanf("%lf %lf",&p[i].x,&p[i].w);
        }
        double l=p[0].x;
        double r=p[m-1].x;
        double ans=three_search(l,r);
        printf("Case #%d: ",cont++);
        printf("%.0lf\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/leekerian/article/details/82595613