Saturday, 27 November 2021

Minimal Square Problem !!!

 PROBLEM 18: Minimal Square


Problem Reference : Codeforces 

Find the minimum area of a square land on which you can place two identical rectangular a×b houses. The sides of the houses should be parallel to the sides of the desired square land.

Formally,

  • You are given two identical rectangles with side lengths a and b (1a,b100) — positive integers (you are given just the sizes, but not their positions).
  • Find the square of the minimum area that contains both given rectangles. Rectangles can be rotated (both or just one), moved, but the sides of the rectangles should be parallel to the sides of the desired square.

Two rectangles can touch each other (side or corner), but cannot intersect. Rectangles can also touch the sides of the square but must be completely inside it. You can rotate the rectangles. Take a look at the examples for a better understanding.


Input:

The first line contains an integer t (1t10000) —the number of test cases in the input. Then t test cases follow.

Each test case is a line containing two integers ab (1a,b100) — side lengths of the rectangles.


Output:

Print t answers to the test cases. Each answer must be a single integer — minimal area of square land, that contains two rectangles with dimensions a×b.


Solution:


  1. t = int(input())
  2. while t > 0 :
  3. a1,b1 = input().split()
  4. a = int(a1)
  5. b = int(b1)
  6. side = min(max(a*2,b),max(a,b*2))
  7. print(side*side)
  8. t -= 1



This above solution is in python language.



Example
input
8
3 2
4 2
1 1
3 1
4 7
1 3
7 4
100 100
output
16
16
4
9
64
9
64
40000

No comments:

Post a Comment

Rearrange an array with O(1) extra space

  PROBLEM 61:  Rearrange an array with O(1) extra space (For best view experience, view in windows version) Problem Reference : GeeksForGeek...