Alex Origami Squares#7612

Alex Origami Squares (Editorial)

If necessary, rotate the rectangle so that its width is no smaller than its height. Consider all possible ways of arranging the three squares.

Case 1. The side length of each square is :

Case 2. If w < 3h, the side length of each square is :

Case 3. If w \ge 3h, the side length of each square is :

It remains to choose the maximum possible side length among the three cases.

max(h / 2, h) = h
max(h / 2, w / 3)

Algorithm implementation

Read the input data.

scanf("%lf %lf",&w,&h);

Normalize the rectangle so that its width is no smaller than its height .

if (w < h)
  temp = w, w = h, h = temp;

Compute and print the answer.

if (w >= 3*h) res = h;
else res = max(h/2, w/3);
printf("%.3lf\n",res);

Java implementation

import java.util.*;

public class Main
{
  public static void main(String[] args)
  {
    Scanner con = new Scanner(System.in);
    double w = con.nextDouble();
    double h = con.nextDouble();
    if (w < h)
    {
      double temp = w;
      w = h;
      h = temp;
    }
    double res;
    if (w >= 3*h) res = h;
    else res = Math.max(h/2, w/3);
    
    System.out.printf("%.3f\n",res);      
  }
}

Python implementation

Read the input data.

w, h = map(float, input().split())

Normalize the rectangle so that its width is no smaller than its height .

if w < h:
  w, h = h, w

Compute and print the answer.

if w >= 3 * h:
  res = h
else:
  res = max(h / 2, w / 3)
print(res)