C. Three Base Stations

http://codeforces.com/problemset/problem/51/C

两次二分+贪心, 被卡精度卡了好久,发现整数点的话,结果不是整数就是 xxx.5的直接整数解决,就不会有精度问题啦。

以后注意利用题目条件, double代码留作纪念。

View Code
const int MM = 511111;
const double esp=1e-8;
const double lep=0.0000001;
#define  debug puts("wrong")
#define clr(a) memset(a,0,sizeof(a))
int N,M,mm;
int x[MM];
double xx[MM];

void get_data() {
    int i,j,k;
    for(i=0;i<N;i++) {
        scanf("%d",&x[i]);
    }
    sort(x,x+N);
    mm=1;
    for(i=1;i<N;i++) if(x[i]!=x[i-1]) x[mm++]=x[i];
    N=mm;
    for(i=0;i<N;i++) xx[i]=(double)(1.0*x[i]);
//    for(i=0;i<N;i++) printf("%.2lf ",xx[i]); printf("\n");
}
int find(double val) {
    int l=0,r=N-1,mid;
    while(l<=r) {
        mid=(l+r)>>1;
        if((xx[mid]-val)>esp) r=mid-1;
        else l=mid+1;
    }
    return l;
}
void solve() {
    int i,j,k,id, tt;
    bool ff=false;
    double ans, tmp, sum, d, a0=0.00,a1=0.00,a2=0.00;
    double l=0, r=xx[N-1], mid;
    while((r-l)>esp) {
        d=l+r; mid=d/2.0;
//        if(!ff) {printf("%lf\n",mid);ff=true;}
        tmp=xx[0]+d;
        a0=xx[0]+mid;
        if(tmp+esp>=xx[N-1]) {r=mid-esp;continue;}
        id=find(tmp); //if(!ff) {printf("%d\n",id);ff=true;}

        tmp=xx[id]+d;
        a1=xx[id]+mid;

        if(tmp+esp>=xx[N-1]) {r=mid-esp;continue;}
        id=find(tmp);
//        if((l-0.5)<esp) printf("%d\n",id);
        if(xx[id]+d+esp>=xx[N-1]) {    a2=xx[id]+mid;r=mid-esp;}
        else l=mid+esp;
    }
    printf("%lf\n",l);
    a0=xx[0]+l;
    tmp=xx[0]+(double)(l*2.0);
//    printf("%lf\n",tmp);
    id=find(tmp);
    if(tmp+lep>=xx[id] && id+1<N) id++;
    a1=xx[id]+l;
    tmp=xx[id]+(double)(l*2.0);
//    printf("%lf\n", tmp);
    tt=find(tmp);
    if(tmp+lep>=xx[tt] && tt+1<N) tt++;
    a2=xx[tt]+l;
//    printf("%d %d\n",id,tt);
    printf("%lf %lf %lf\n",a0,a1,a2);
}

int main() {
    while(scanf("%d",&N)!=EOF) get_data(),solve();
    return 0;
}
/*
6 1 3 100 105 1000 1010
10 1 2 3 4 5 6 7 8 9 10
*/
原文地址:https://www.cnblogs.com/zhang1107/p/3027933.html