cppAcademy
FROM LEARNING TO COMPETING

Expert.
Put your skills to the test.

Bulgarian Informatics Group A · 2007–2026

274 independently explained problems: 54 Municipal, 54 Regional, 109 National Olympiad and 57 Autumn Tournament tasks. Difficulty labels are learning estimates, not official ratings. Some events share Senior/IATI or Group A/B problem sets. Special formats, scoring limitations and source clarifications are explained on affected tasks. Existing shared National-round problems keep their permanent URLs.
GROUP A2010 · MediumProjection geometry

Minimum Rectangle on Four Parallel Rails

Autumn Tournament (2010)

Submission name: abcd.cpp

Problem summary

Four points can slide independently along four parallel infinite rails. Consecutive rail distances are p,q,r. Decide whether the points can become rectangle vertices and find the smallest possible area.

Input

Three positive decimals p q r, each with at most two fractional digits.

Output

0 if impossible; otherwise the minimum area with four decimals.

Constraints

0<p,q,r≤10000.

Worked example

An independently prepared worked example; constructive outputs may have other valid forms.

Input

3 2.0 3.00

Output

30.0000

Solution analysis

  1. Use the direction perpendicular to the rails as the x-axis. The four x-coordinates are0,p,p+q,p+q+r. Diagonals of a rectangle have the same midpoint, so smallest+largest must equal the sum of the two middle coordinates. This requires p=r.
  2. When p=r, take the two side vectors from the leftmost vertex. Their x-components are p and p+q. If their y-components are u and v, perpendicularity requires uv=−p(p+q).
  3. The area is |pv−(p+q)u|. Its two positive terms have product p²(p+q)², so their sum is at least2p(p+q), attained with suitable opposite-signed y-components. For3,2,3 the minimum is2·3·5=30.

Why it works

The midpoint condition proves impossibility when p≠r. When p=r, the arithmetic–geometric mean inequality proves area≥2p(p+q), and choosing the y-components to make its terms equal realizes a rectangle with that area. Thus the bound is attainable and optimal.

Complexity: O(1) time and space; decimal input has bounded length. Reading cents as integers makes the equality test exact.

Watch out. Do not compare floating-point p and r with exact equality. The points may form a rotated rectangle; requiring sides parallel to rails would miss valid solutions.

Commented C++17 solution

An independently written complete program with standard headers.

#include<iomanip>
#include<iostream>
#include<string>
using namespace std;
long long cents(string text){
    size_t dot=text.find('.');
    if(dot==string::npos){
        return stoll(text)*100;
    }
    string fraction=text.substr(dot+1);
    while(fraction.size()<2){
        fraction.push_back('0');
    }
    return stoll(text.substr(0,dot))*100+stoll(fraction);
}
int main(){
    string first,middle,last;
    cin>>first>>middle>>last;
    long long p=cents(first);
    long long q=cents(middle);
    long long r=cents(last);
    if(p!=r){
        cout<<0<<'\n';
        return 0;
    }
    // Products of cent distances must be divided by10000.
    long double area=2.0L*p*(p+q)/10000;
    cout<<fixed<<setprecision(4)<<area<<'\n';
}

Source: Arena Olimpiici ↗