cppAcademy
FROM LEARNING TO COMPETING

Proficient.
Put your skills to the test.

Bulgarian Informatics Group B · 2009–2026

217 independently explained problems: 54 Municipal, 54 Regional and 109 National. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group B also includes challenging national-level problems. National-round tasks shared by Groups A and B are included. Years follow Arena’s competition-season labels. Source clarifications and special grader requirements are noted on affected tasks.
GROUP B2012 · EasyGeometry

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

  1. 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.
  2. The inequality (x−y)²≥0 gives 2xy≤x²+y²=R². Equality is attained at x=y=R/√2, so the answer is simply R².
  3. 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.

Complexity: O(1) time and O(1) space.

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

Source: Arena Olimpiici ↗