Topic Link: Click to open the link
The main topic: there is a wine barrel capacity for VC, there are VW's wine, and now use VB water to brush wine barrels, every time the inner wall of the barrel will leave a VR liquid, up to brush k times, ask how to brush wine barrels, can let the wine barrel inside the least.
If VB+VW < Vc, then direct output 0
In other cases, it is guaranteed that the water will be poured out. Therefore, the remaining liquid in the final bucket is VR, as long as the amount of wine in the VR is guaranteed to be the smallest, then the remaining wine is the least.
It can be assumed that the water used is x1,x2,x3,,,, calculate the concentration after each brush pass.
First time brushes: Vw/(VW+X1)
Second time brush: Vw/(VW+X1) * Vr/(VR+X2)
Third time Brush: Vw/(VW+X1) * VR/(VR+X2)* VR/(VR+X3)
This also can be seen, if scrubbing k times, then the final concentration is Vw/(VW+X1)* VR/(VR+X2) * VR/ (VR+X3),,,,* VR/(VR+XK)
So how do you make sure it's worth the least? If we can determine a x1, then x2+x3,,, +xk = vb-x1, such conditions how to guarantee VR/( vr+x2) * VR/(VR+X3),,,, * VR/(VR+XK) as small as possible, we can find if x2 = x3 = x4, = XK The calculated result will be smaller than the difference, if the value of x is (VB-X1)/(k-1), then the larger the K, the smaller the value will be. (VB-X1)/(k-1), the larger the value, the smaller the values are.
So choose the most scrub times, from 2nd to K times, each time the same water, then the rest is X1 how to determine.
If x1 increases, then Vw/(VW+X1) will decrease,(vb-x1)/(k-1) will decrease, (vr/) ^ (vr+x) will increase, the total concentration is not determined, so use three points to find a minimum result.
Attention
1, three minutes when the barrel has VW, pay attention to three points of the upper and lower bounds.
2, in the calculation of the concentration, the water added to the bucket is calculated by the vb-x, but this value can not be super-VC-VR
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm>using namespace std; Define EQS 1e-9int k;d ouble vb, VW, VR, VC;d ouble Solve (double x) {Double ans = vw/(vw+x); if (k > 1) {Double y = min ((vb-x)/(k-1), VC-VR); for (int i = 1; i < K; i++) ans *= vr/(vr+y); } return ans; int main () {while (scanf ("%d", &k)! = EOF) {if (k = = 0) break; scanf ("%lf%lf%lf%lf", &vb, &VW, &VR, &VC); if (Vr-vw-vb > EQs) {printf ("0\n"); Continue; } Double low = max (0.0,VR-VW), MID1, mid2, high = min (VB,VC-VW); while (Low + EQs < high) {MID1 = (low + high)/2.0; Mid2 = (mid1 + high)/2.0; if (Solve (MID1) > Solve (MID2)) {low = MID1; } else high = Mid2; } printf ("%d", k); printf ("%.2f", high); if (k > 1) high = min (Vc-vr, (Vb-low)/(k-1)); for (int i = 1; i < K; i++) {printf ("%.2f", high); } printf ("\ n"); } return 0;}
Copyright NOTICE: Reprint Please specify source: Http://blog.csdn.net/winddreams
Poj3296--rinse (three points)