LightOJ 1297 - Largest Box【二分】


1297 - Largest Box
PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

In the following figure you can see a rectangular card. The width of the card is W and length of the card is L and thickness is zero. Four (x*x) squares are cut from the four corners of the card shown by the black dotted lines. Then the card is folded along the magenta lines to make a box without a cover.

LightOJ 1297 - Largest Box【二分】_第1张图片

Given the width and height of the box, you will have to find the maximum volume of the box you can make for any value of x.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case starts with a line containing two real numbers L and W (0 < L, W < 100).

Output

For each case, print the case number and the maximum volume of the box that can be made. Errors less than 10-6 will be ignored.

Sample Input

Output for Sample Input

3

2 10

3.590 2.719

8.1991 7.189

Case 1: 4.513804324

Case 2: 2.2268848896

Case 3: 33.412886

 

PROBLEM SETTER: SHAHRIAR MANZOOR
SPECIAL THANKS: JANE ALAM JAN (DESCRIPTION, SOLUTION, DATASET)
解题思路:
    利用倒数的性质,先对体积函数对X求导,然后就可以利用二分。左区间为0,右区间为W/2。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
double x,y;
bool F(double x1)
{
	if((x*y-4*x1*(x+y)+12*x1*x1)>0)
	{
		return true;
	}
	return false;
}
int main()
{
	int T;
	int ans=0;
	scanf("%d",&T);
	while(T--)
	{
		ans++;

		scanf("%lf%lf",&x,&y);
		double L,R,mid;
		L=0;
		R=y/2;
		printf("Case %d: ",ans);
		while(R-L>1e-7)
		{
			mid=(L+R)/2;
			if(F(mid))
			{
				L=mid;
			}
			else
			{
				R=mid;
			}
		}
		printf("%.8lf\n",R*(x-2*R)*(y-2*R));
	} 
	return 0;
} 

你可能感兴趣的:(数学,二分,lightoj)