LA 3485 Bridge

时间:2021-11-01 16:23:52

自适应辛普森公式模板。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std; double a; double F(double x)///需要积分的式子。
{
return sqrt(1 + 4 * a * a * x * x);
} double simpson(double a, double b)///模板
{
double c = a + (b - a) / 2;
return (F(a) + 4 * F(c) + F(b)) * (b - a) / 6;
} double asr(double a, double b, double eps, double A)///模板
{
double c = a + (b - a) / 2;
double L = simpson(a, c), R = simpson(c, b);
if(fabs(L+R-A) < 15*eps) return L+R+(L+R-A) / 15.0;
return asr(a, c, eps / 2, L) + asr(c, b, eps / 2, R);
} double asr(double a, double b, double eps)///模板
{
return asr(a, b, eps, simpson(a, b));
} double parabola_arc_length(double w, double h)
{
a = 4.0 * h / (w * w);
return asr(0, w / 2, 1e-5) * 2;
} int main()
{
int t, cas = 1;
scanf("%d", &t);
while(t --)
{
int D, H, B, L;
scanf("%d%d%d%d", &D, &H, &B, &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 - x) / 2;
if(parabola_arc_length(D1, m) < L1) x = m;
else y = m;
}
if(cas > 1) puts("");
printf("Case %d:\n%.2lf\n", cas ++, H - x);
}
return 0;
}