Largest Rectangle in a Semicircle
National Olympiad — Municipal round (2012 season)
Submission name: maxs.cpp
Problem summary
Find the largest area of a rectangle inscribed in a semicircle of radius R, with its bottom side on the diameter.
Input
One positive real radius R, possibly in scientific notation.
Output
The maximum area rounded to three decimal places.
Constraints
The statement gives no explicit numerical bound for R; it is supplied as a valid real number.
Worked example
An independently prepared example for this explanation.
Input
1.23
Output
1.513
Solution analysis
- Let x be half the rectangle width and y its height. Its upper corners lie on the circle, so x²+y²=R² and its area is 2xy.
- The inequality (x−y)²≥0 gives 2xy≤x²+y²=R². Equality is attained at x=y=R/√2, so the answer is simply R².
- For R=1.23, the area is 1.5129, which is printed as 1.513. No numerical optimization or search is necessary.
Why it works
Every inscribed rectangle satisfies the bound 2xy≤R². Choosing x=y=R/√2 realizes a rectangle in the semicircle with area R², proving optimality.
Watch out. The rectangle width is 2x, not x. Format the result with three digits after the decimal point.
Commented C++17 solution
An independently written complete program with standard headers.
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
long double radius;
cin>>radius;
// The optimal half-width and height are both R / sqrt(2).
cout<<fixed<<setprecision(3)<<radius*radius<<'\n';
}