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);
    }
}