LA 3485 自适应Simpson积分 解题报告

A suspension bridge suspends the roadway from huge main cables, which extend from one end of thebridge to the other. These cables rest on top of high towers and are secured at each end by anchorages.The towers enable the main cables to be draped over long distances.
Suppose that the maximum distance between two neighboring towers is D, and that the distancefrom the top of a tower to the roadway is H. Also suppose that the shape of a cable between any twoneighboring towers is the same symmetric parabola (as shown in the figure). Now given B, the lengthof the bridge and L, the total length of the cables, you are asked to calculate the distance between theroadway and the lowest point of the cable, with minimum number of towers built (Assume that thereare always two towers built at the two ends of a bridge).
这里写图片描述

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 ≤T ≤ 10) which is the number of test cases. T test cases follow, each preceded by a single blank line.
For each test case, 4 positive integers are given on a single line.
D - the maximum distance between two neighboring towers;
H - the distance from the top of a tower to the roadway;
B - the length of the bridge; and
L - the total length of the cables.
It is guaranteed that B ≤ L. The cable will always be above the roadway.

Output

Results should be directed to standard output. Start each case with ‘Case #:’ on a single line, whereis the case number starting from 1. Two consecutive cases should be separated by a single blankline. No blank line should be produced after the last test case.
For each test case, print the distance between the roadway and the lowest point of the cable, as isdescribed in the problem. The value must be accurate up to two decimal places.

Sample Input

2
20 101 400 4042
1 2 3 4

Sample Output

Case 1:
1.00
Case 2:
1.60

【解题报告】
讲道理这道题不难,但我一直WA
和AC代码本机对拍都没有问题。。。
可能是被卡精度了。。。
欢迎查错,物质奖励

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
#define eps 1e-12

int D,H,B,L;
double a;

double f(double x)
{
    return sqrt(1+4*a*a*x*x);
}
double calc(double len,double fL,double fM,double fR) 
{
    return (fL+4*fM+fR)*len/6;
}
double Simpson(double L,double M,double R,double fL,double fM,double fR,double sqr) 
{
    double M1=(L+M)/2,M2=(M+R)/2;
    double fM1=f(M1),fM2=f(M2);
    double g1=calc(M-L,fL,fM1,fM),g2=calc(R-M,fM,fM2,fR);
    if(fabs(sqr-g1-g2)<=eps) return g1+g2;
    return Simpson(L,M1,M,fL,fM1,fM,g1)+Simpson(M,M2,R,fM,fM2,fR,g2);
}
int main()
{
    int t,cas=0;
    for(scanf("%d",&t);t;--t)
    {
        scanf("%d%d%d%d",&D,&H,&B,&L);
        int n=(B+D-1)/D;
        double ll=(double)L/n,dd=(double)B/n/2;
        double l=0,r=(double)H;
        while(r-l>eps)
        {
            double mid=(l+r)/2; 
            a=mid/dd/dd;//(mid=a*d*d)
            double m=dd/2;
            if(2*Simpson(0,m,dd,f(0),f(m),f(dd),calc(dd,f(0),f(m),f(dd)))>ll) r=mid;
            else l=mid;
        }
        printf("Case %d:\n%.2f\n\n",++cas,H-l);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值