LA3485 Bridge

时间:2022-02-27 15:11:06

题意

PDF

分析

以《训练指南》上的分析为准。

二分法时间复杂度\(O(\log v)\)

代码

#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
    rg T data=0,w=1;rg char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
    while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
    return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;

double F(double a,double x){
    double a2=a*a,x2=x*x;
    return (x*sqrt(a2+x2)+a2*log(fabs(x+sqrt(a2+x2))))/2;
}
double parabola_arc_length(double w,double h){
    double a=4*h/(w*w);
    double b=1/(2*a);
    return (F(b,w/2)-F(b,0))*4*a;
}
int main(){
//  freopen(".in","r",stdin),freopen(".out","w",stdout);
    for(int T=read<int>(),kase=1;kase<=T;++kase){
        int D,H,B,L;
        read(D),read(H),read(B),read(L);
        int n=(B+D-1)/D;
        double D1=double(B)/n;
        double L1=double(L)/n;
        double x=0,y=H;
        while(y-x>1e-5){
            double m=(x+y)/2;
            parabola_arc_length(D1,m)<L1?x=m:y=m;
        }
        if(kase>1) puts("");
        printf("Case %d:\n%.2lf\n",kase,H-x);
    }
    return 0;
}