Official Analysis (Java)

Video Solution

By Maggie Liu

Video Solution Code

Explanation

We can first find the smallest rectangle that covers both pastures. This rectangle will need to cover the smaller of the left sides of both pastures and the larger of the right sides of both pastures. It will also need to cover the smaller of the bottom sides and the larger of the top sides of both pastures. The smallest square has a side length equal to the longer side length of the rectangle.

Implementation

Time Complexity: O(1)\mathcal{O}(1)

import sys
sys.stdin = open("square.in", "r")
sys.stdout = open("square.out", "w")
x1, y1, x2, y2 = map(int, input().split())
x3, y3, x4, y4 = map(int, input().split())
# find the sides of the smallest rectangle covering both pastures
left = min(x1, x3)

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!