uva 1356 - Bridge(积分+二分)

题目链接:uva 1356 - Bridge

题目大意:在一座长度为B的桥上建若干个塔,塔的间距不能超过D,塔的高度为H,塔之间的绳索形成全等的抛物线。绳索的总长度为L。问在建最少塔的情况下,绳索的最下段离地面的高度。

解题思路:贪心的思想求出最少情况下建立的塔数。

二分高度,然后用积分求出两塔之间绳索的长度。

C++ 积分

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

using namespace std;

double f (double a, double x) {
    double aa = a * a, xx = x * x;;
    return (x * sqrt(aa + xx) + aa * log(fabs(x + sqrt(aa + xx)))) / 2;
}

double parabola_length (double w, double h) {
    double a = 4 * h / (w * w);
    double b = 1.0 / (2 * a);
    return (f(b, w/2) - f(b, 0)) * 4 * a;
}

double bsearch (double l, double r, double d, double v) {
    while (r - l > 1e-5) {
        double mid = (r + l) / 2;
        if (parabola_length(d, mid) < v)
            l = mid;
        else
            r = mid;
    }
    return l;
}

int main () {
    int cas;
    scanf("%d", &cas);
    for (int kcas = 1; kcas <= cas; kcas++) {
        int D, H, B, L;
        scanf("%d%d%d%d", &D, &H, &B, &L);

        int n = (B + D - 1) / D;
        double d = B * 1.0 / n;
        double l = L * 1.0 / n;

        if (kcas > 1)
            printf("\n");
        printf("Case %d:\n%.2lf\n", kcas, (double)H - bsearch(0, H, d, l));
    }
    return 0;
}
C++ 辛普森

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

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) / 2;
    return (f(a) + 4*f(c) + f(b)) * (b-a) / 6;
}

double asr (double a, double b, double eps, double S) {
    double c = (a + b) / 2;
    double L = simpson(a, c), R = simpson(c, b);
    if (fabs(L+R-S) <= eps * 15)
        return L + R + (L + R - S) / 15;
    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_length (double w, double h) {
    A = 4 * h / (w * w);
    return asr(0, w / 2, 1e-5) * 2;
}

double bsearch (double l, double r, double d, double v) {
    while (r - l > 1e-5) {
        double mid = (r + l) / 2;
        if (parabola_length(d, mid) < v)
            l = mid;
        else
            r = mid;
    }
    return l;
}

int main () {
    int cas;
    scanf("%d", &cas);
    for (int kcas = 1; kcas <= cas; kcas++) {
        int D, H, B, L;
        scanf("%d%d%d%d", &D, &H, &B, &L);

        int n = (B + D - 1) / D;
        double d = B * 1.0 / n;
        double l = L * 1.0 / n;

        if (kcas > 1)
            printf("\n");
        printf("Case %d:\n%.2lf\n", kcas, (double)H - bsearch(0, H, d, l));
    }
    return 0;
}

uva 1356 - Bridge(积分+二分),布布扣,bubuko.com

时间: 2024-08-08 01:27:59

uva 1356 - Bridge(积分+二分)的相关文章

UVA 1356 - Bridge(自适应辛普森)

UVA 1356 - Bridge 题目链接 题意:一个桥长为B,桥上建电线杆,杆高为H,两杆之间距离不超过D,电线杆总长为L,杆子都是等距的,现在建最少的电线杆,问这时候电线离地面高度是多少 思路:二分高度,求出电线长,判断长度够不够即可,那么问题就变成怎么求弧长 求弧长公式为∫w/201+(f′(x)2)??????????√, 建立坐标系使得f(x)=ax2,带入点(w/2, h)求出a,得到方程 那么问题就变成怎么求这个积分了 利用辛普森自适应法,去求即可 代码: #include <c

UVA - 1356 Bridge

Description A suspension bridge suspends the roadway from huge main cables, which extend from one end of the bridge 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

UVA 1487 - Volume(积分)

UVA 1487 - Volume 题目链接 题意:给定r,h,求图中该图形体积 思路:积分题,用总面积减去重合体积,一个卦限重合体积为∫∫Dr2?h2??????√,求得r2x?13x3,然后这个面积乘8就是总重合体积,但是注意题目中可能存在2r>h,对于这种情况实际上中间重合部分等于多一个立方体,求出该立方体高就能求出体积了,画图很容易看出高为r2?h22,算出重合体积再用总体积减去即可 代码: #include <cstdio> #include <cstring> #

uva 1356/LA 3485 Bridge 题解

题意:有一座桥,桥上等距摆若干个塔,高度H,宽度不计.相邻两个塔距离不超过D.有一个绳索,总长度为L,桥的长度为L,两个塔之间的绳索成全等的抛物线.求建最少的塔的时候绳索下端离地高度y. 白书上的例题..我基本上是抄的代码. 间隔数n=ceil(B/D),每个间隔宽度D'=B/n,之间的绳索长度L'=L/n.则抛物线的宽度已知,即D'. 根据微积分知识,可导函数f(x)在区间[a,b]上的弧长为\(\int_{b}^{a} \sqrt{1+[f'(x)]^2}dx\) . 则此时抛物线的长度只与

HDU 5954 - Do not pour out - [积分+二分][2016ACM/ICPC亚洲区沈阳站 Problem G]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5954 Problem DescriptionYou have got a cylindrical cup. Its bottom diameter is 2 units and its height is 2 units as well.The height of liquid level in the cup is d (0 ≤ d ≤ 2). When you incline the cup t

UVA 12124 - Assemble(二分)

题目链接:点击打开链接 题意: 给n个组件来组装电脑, 每个组件有4个属性:种类.名称.价格.品质. 要求每种组件买一个, 求在不超过预算的情况下, 品质最低的品质尽量大. 思路:很显然, 二分最低品质, 然后判断是否可行, 属于二分找上界,在二分时有一个小技巧用来处理当区间相差1时的情况. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #i

Uva 11346 Probability 积分

化成反比函数求积分 G - Probability Time Limit: 1 sec Memory Limit: 16MB Consider rectangular coordinate system and point L(X,Y) which is randomly chosen among all points in the area A which is defined in the following manner: A = {(x,y) | x is from interval [

UVA - 12338 Anti-Rhyme Pairs 二分+hash

题目链接:https://vjudge.net/problem/UVA-12338 题意: 给你n个串 问你任意两个串的最大公共前缀长度是多少 题解: 二分+hash 思路很明显,我最近用来写hash 很鸡肋 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define ls i<<1 #define rs ls

UVA 1149 Bin Packing 二分+贪心

A set of n 1-dimensional items have to be packed in identical bins. All bins have exactly the samelength l and each item i has length li ≤ l. We look for a minimal number of bins q such that• each bin contains at most 2 items,• each item is packed in