ZOJ 3913 Bob wants to pour water 二分

来源:互联网 发布:maya2015 mac破解版 编辑:程序博客网 时间:2024/05/23 11:50

题意:给你一个无限高的长方体,里面有一些小的长方体和圆球,他们互不相交,问你加入一定体积的水后,水面的高度。

思路:二分水面高度,判断水的体积即可。

这题我写的有点麻烦,减小了精度损失,而且别的地方也加了二分,但是貌似这题并不需要。。。

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <iostream>using namespace std;const int maxn = 100000 + 10;const double eps = 1e-8;const double PI = acos(-1.0);int dcmp(long double x){    if(fabs(x) < eps) return 0;    return x < 0 ? -1 : 1;}struct Cuboid{    double center;    double width, length, height;    double up, low;    void read(){        scanf("%lf%lf%lf%lf", &center, &width, &length, &height);        up = center + height / 2;        low = center - height / 2;    }    bool operator < (const Cuboid &rhs) const{        return dcmp(up - rhs.up) < 0;    }};struct Sphere{    double center;    double r;    double up, low;    void read(){        scanf("%lf%lf", &center, &r);        up = center + r;        low = center - r;    }    bool operator < (const Sphere &rhs) const{        return dcmp(up - rhs.up) < 0;    }};double w, l, v;int n, m;Cuboid cuboid[maxn];Sphere sphere[maxn];double cuboid_sum[maxn];double sphere_sum[maxn];double Cuboid_Volume(int ind){    return cuboid[ind].height * cuboid[ind].length * cuboid[ind].width;}double Sphere_Volume(int ind){    return 4.0 * sphere[ind].r * sphere[ind].r * sphere[ind].r / 3.0;}double Cuboid_Volume_Part(int ind, double cur_h){    return (cur_h - cuboid[ind].low) * cuboid[ind].length * cuboid[ind].width;}double Sphere_Volume_Part(int ind, double cur_h){    if(cur_h > sphere[ind].center){        double hi = sphere[ind].up - cur_h;        return Sphere_Volume(ind) - (sphere[ind].r * hi * hi - hi * hi * hi / 3.0);    }    double hi = cur_h - sphere[ind].low;    return (sphere[ind].r * hi * hi - hi * hi * hi / 3.0);}int find_cuboid_top(double cur_h){    int ind = 0;    int le = 1, ri = n;    while(le <= ri){        int mid = (le + ri) / 2;        if(dcmp(cuboid[mid].up - cur_h) <= 0){            ind = mid;            le = mid + 1;        }        else ri = mid - 1;    }    return ind;}int find_sphere_top(double cur_h){    int ind = 0;    int le = 1, ri = m;    while(le <= ri){        int mid = (le + ri) / 2;        if(dcmp(sphere[mid].up - cur_h) <= 0){            ind = mid;            le = mid + 1;        }        else ri = mid - 1;    }    return ind;}bool Judge(double cur_h){//空间体积小于水的体积返回Ture    double cur_cuboid_volume = 0;    double cur_sphere_volume = 0;    int cuboid_ind = find_cuboid_top(cur_h);    int sphere_ind = find_sphere_top(cur_h);    cur_cuboid_volume += cuboid_sum[cuboid_ind];    cur_sphere_volume += sphere_sum[sphere_ind];    for(int i = cuboid_ind + 1; i <= n; i++){        if(dcmp(cuboid[i].low - cur_h) >= 0) continue;        cur_cuboid_volume += Cuboid_Volume_Part(i, cur_h);    }    for(int i = sphere_ind + 1; i <= m; i++){        if(dcmp(sphere[i].low - cur_h) >= 0) continue;        cur_sphere_volume += Sphere_Volume_Part(i, cur_h);    }    if(dcmp(w * l * cur_h - (cur_cuboid_volume + cur_sphere_volume * PI) - v) < 0) return true;    return false;}void solve(){    scanf("%lf%lf%lf%d%d", &w, &l, &v, &n, &m);    memset(cuboid_sum, 0, sizeof(cuboid_sum));    memset(sphere_sum, 0, sizeof(sphere_sum));    for(int i = 1; i <= n; i++) cuboid[i].read();    for(int i = 1; i <= m; i++) sphere[i].read();    sort(cuboid + 1, cuboid + n + 1);    sort(sphere + 1, sphere + m + 1);    for(int i = 1; i <= n; i++) cuboid_sum[i] = cuboid_sum[i-1] + Cuboid_Volume(i);    for(int i = 1; i <= m; i++) sphere_sum[i] = sphere_sum[i-1] + Sphere_Volume(i);    double le = v / (w * l), ri = (v + cuboid_sum[n] + sphere_sum[m] * PI) / (w * l);    int cnt = 0;    while(le + eps < ri){        double mid = (le + ri) / 2;        if(Judge(mid)) le = mid;        else ri = mid;    }    printf("%lf\n", le);}int main(){    //freopen("in.txt", "r", stdin);    int T;    scanf("%d", &T);    while(T--) solve();    return 0;}/*11 1 1 2 01 1 1 12.5 1 1 1*/
0 0