现在的位置: 首页 > 综合 > 正文

SGU114 Telecasting station 三分

2013年03月25日 ⁄ 综合 ⁄ 共 723字 ⁄ 字号 评论关闭

WA点: 把P也当浮点数输入了...wa test1 一次...

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const double eps=1e-8;
const double inf=1e10;
double X[15005];
int P[15005];
int N;
int sgn(double x)
{
    return x<-eps?-1:x>eps;
}
double Fabs(double x)
{
    return (sgn(x)<0)?-x:(x>eps?x:0);
}
double function(double x)
{
    double ret=0;
    int i;
    for(i=0;i<N;++i)
    {
        ret+=Fabs(X[i]-x)*P[i];
    }
    return ret;
}
double work()
{
    double l=-1.0,r=50005.0;
    double m,tm;
    while((r-l)>eps)
    {
        m=(l+r)/2;
        tm=(m+r)/2;
//        m=(l+tm)/2;
        if(function(m)<function(tm))
            r=tm;
        else
            l=m;
    }
    return l;
}
int main()
{
    int i;
    while(scanf("%d",&N)!=EOF)
    {
        for(i=0;i<N;++i)
        {
            scanf("%lf %d",&X[i],&P[i]);
        }
//        for(i=0;i<N;++i)
//            printf("%f %f\n",X[i],P[i]);
        printf("%.5f\n",work());
    }
    return 0;
}

 

抱歉!评论已关闭.