HDU6487 Overflow

本文介绍了一道编程题的解决方案,题目要求计算在一定水量的水桶中放入多个正方体木块后,水面最终的高度。通过二分查找的方法,结合木块的密度和体积,判断木块的浮沉状态,从而精确计算出水面高度。

http://acm.hdu.edu.cn/showproblem.php?pid=6487

Problem Description
Kayaking is a naughty boy and he loves to play water. One day, Kayaking finds a bucket. The bottom area of the bucket is S and the height is H. Initially, there is V volume water in the bucket. What makes Kayaking happy is that there are N cube woods beside the bucket. The side length of the i-th cube woods is L[i] and its density is P[i]. Kayaking wants to put all the cube woods to the bucket. And then he will put a cover at the top of the bucket. But CoffeeDog doesn’t allow unless Kayaking can tell CoffeeDog the height of the water in the bucket after Kayaking put all the cuboid woods to the bucket. Could you help him?
It is guaranteed that the cube woods aren’t overlapping. And after putting the wood to the bucket, the bottom of the wood is parallel to the bottom of the bucket.

题意:给定n个正方体,放在原有一定水量的水桶里,问最后的水面高度。
思路:密度小于1,漂浮,在水中的体积等于其质量,密度大于等于1,悬浮或者沉底,但是不一定能够完全浸没,所以没有办法直接求出最终的水面体积。
那么二分最后的水高度,如果当前检查的高度h大于实际高度,那么 h ∗ 底 面 积 S > 原 始 V + 木 块 排 水 v , 反 之 小 于 h*底面积S>原始V+木块排水v,反之小于 hS>V+v

#include<bits/stdc++.h>
using namespace std;
#define maxn 10010
#define eps 1e-5

int T,n;
double l[maxn],p[maxn],S,H,V;

bool check(double H)
{
    double v=0;
    for(int i=1;i<=n;i++)
    {
        if(p[i]<1)v+=p[i]*l[i]*l[i]*l[i];
        else v+=min(l[i],H)*l[i]*l[i];
    }
    return v+V<H*S;
}

int main()
{
    freopen("input.in","r",stdin);
    cin>>T;
    while(T--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)scanf("%lf%lf",&l[i],&p[i]);
        cin>>S>>H>>V;
        double l=V/S,r=H,mid;
        while((r-l)>eps)
        {
            mid=(l+r)/2;
            if(check(mid))r=mid;
            else l=mid;
        }
        printf("%.2f\n",mid);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值