Area
National Olympiad — Municipal round (2014 season)
Submission name: area.cpp
Problem summary
Use four straight fence sections, three of length a and one of length b, to enclose the largest possible area. If no nondegenerate quadrilateral can be formed, the answer is zero.
Source clarification. The PDF statement does not specify the impossible-closure case separately. The official analysis explicitly defines the answer as zero when 3a≤b: https://infos.infosbg.com/files/Contests/NOI-1/2014/A/analyses/area_analysis.pdf .
Input
The real numbers a and b, each with at most two decimal places.
Output
The maximum area, rounded and formatted to two decimal places.
Constraints
1 ≤ a,b ≤ 1000.
Worked example
Official statement sample.
Input
71.08 38
Output
3770.29
Solution analysis
- A quadrilateral exists only if its longest side is shorter than the sum of the other three. Here the only possible failure is b≥3a, in which case the official analysis specifies zero.
- For fixed side lengths the maximum-area quadrilateral is cyclic. With sides a,a,a,b its cyclic form can be an isosceles trapezoid with parallel bases a and b and legs a. Its height is sqrt(a²−((b−a)/2)²), giving area (a+b)/2 times that height.
- For the sample the bases are 71.08 and 38 and each leg is 71.08. The horizontal offset per leg is 16.54, and the resulting trapezoid area rounds to 3770.29. When a=b this formula gives the square area a².
Why it works
Bretschneider’s formula gives S²=(s−a)^3(s−b)−a³b cos²((A+C)/2), where s=(3a+b)/2. The maximum is attained when opposite angles sum to π, making the subtracted term zero. The isosceles trapezoid with bases a,b and legs a is cyclic and attains this bound whenever b<3a. Otherwise closure with positive area is impossible.
Watch out. Do not apply the square root to a negative radicand when b≥3a. Print exactly two digits after the decimal point.
Commented C++17 solution
An independently written complete program with standard headers.
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main(){
long double a,b;
cin>>a>>b;
long double area=0;
// The maximum is attained by the cyclic isosceles trapezoid.
if(b<3*a){
long double offset=(b-a)/2;
long double height=sqrtl(a*a-offset*offset);
area=(a+b)*height/2;
}
cout<<fixed<<setprecision(2)<<area<<'\n';
}