Swimming Balls
https://vjudge.net/contest/318752#problem/J
如果直接算,各种球的情况都不清楚,因为放一个球之后,水位的变化也会影响之前放入的球,不如,二分最终的水位高度,这样每个球的贡献就有了
#include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> #include <cstring> #define inf 2147483647 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(int i=a;i<=b;++i) //by war //2019.8.16 using namespace std; int T,n; double L,x,y,mid,W,D,V,w[N],r[N],eps=1e-10,ans,pai=3.141592653589793; void in(int &x){int y=1;char c=getchar();x=0;while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}x*=y; } void o(int x){if(x<0){p('-');x=-x;}if(x>9)o(x/10);p(x%10+'0'); }double v(double r){return 4.0000000/3.00000000*pai*r*r*r; }double deal(double h,int i){return pai*(r[i]*r[i]*h-h*h*h/3.0); }double lj(double x,int i){if(x-r[i]>eps)return v(r[i])/2.0+deal(min(r[i],x-r[i]),i);return v(r[i])/2.0-deal(r[i]-x,i); }bool check(double x){double t=V;For(i,1,n){if(1.0000000-w[i]>eps)t+=min(v(r[i])*w[i],lj(x,i));elset+=lj(x,i);}if(t/W/L-x>eps)return 1;return 0; } signed main(){in(T);while(T--){in(n);cin>>W>>L>>D>>V;if(V<eps){puts("0.0000000000");continue;}For(i,1,n)cin>>r[i]>>w[i];x=0;y=D+eps;while(y-x>eps){mid=(x+y)/2.0;if(check(mid))x=mid;elsey=mid;}printf("%.10f\n",mid);}return 0; }