首页 > 代码库 > hdu 4355 Party All the Time (三分)

hdu 4355 Party All the Time (三分)

//函数满足凸函数性质,于是三分
# include <stdio.h>
# include <algorithm>
# include <string.h>
# include <math.h>
using namespace std;
struct node
{
    double x;
    double w;
};
struct node a[50010];
int n;
double cal(double xi)
{
    double res=0;
    for(int i=0; i<n; i++)
    {
        double t=fabs(a[i].x-xi);
        res+=t*t*t*a[i].w;
    }
    return res;
}
int main()
{
    int t,i;
    double ans;
    while(~scanf("%d",&t))
    {
        int cas=0;
        while(t--)
        {
            scanf("%d",&n);
            for(i=0; i<n; i++)
                scanf("%lf %lf",&a[i].x,&a[i].w);
            double l=a[0].x;
            double r=a[n-1].x;
            while(l<=r)
            {
                if(fabs(r-l)<0.0001)
                    break;
                double mid=(l+r)/2;
                double midm=(mid+r)/2;
                if(cal(mid)<cal(midm))
                {
                    ans=cal(mid);
                    r=midm;
                }
                else
                    l=mid;
            }
            printf("Case #%d: ",++cas);
            printf("%.0lf\n",ans);
        }
    }
    return 0;
}

hdu 4355 Party All the Time (三分)