Packing Rectangles

Interview

Time limit1sMemory limit512 MB

Summary
Given four rectangles, find every smallest enclosing axis-parallel rectangle that fits all four without overlap, using the six basic layouts.
Level

Medium7 of 10

Topics
Brute force, Geometry, Implementation, Simulation
Solved
No attempts yet

Problem

Figure 1: The six basic layouts of four rectangles

You are given four rectangles. Find the smallest enclosing rectangle into which all four fit without overlapping. "Smallest" means the one with the least area.

Every rectangle must have its sides parallel to the corresponding sides of the enclosing rectangle. Figure 1 shows the six ways to fit four rectangles together; every other arrangement can be obtained from one of these basic layouts by rotation or reflection, so these six are the only basic layouts.

Each rectangle may be placed in either orientation (rotated by 90°90°). Several different enclosing rectangles with the same minimum area may exist, and you must report all of them.

Input

The input consists of four lines. Each line describes one rectangle by two positive integers: the lengths of its two sides. Each side length is between 11 and 5050, inclusive.

Output

Print one line more than the number of solutions. The first line contains a single integer: the minimum area of an enclosing rectangle. Each of the following lines describes one solution by two numbers pp and qq with p≤qp \le q. These lines must be sorted in ascending order of pp and must all be distinct.

Examples3

  1. Example 1

    Input
    1 2
    2 3
    3 4
    4 5
    
    Expected output
    40
    4 10
    5 8
    
  2. Example 2

    Input
    1 1
    1 1
    1 1
    1 1
    
    Expected output
    4
    1 4
    2 2
    
  3. Example 3

    Input
    50 50
    50 50
    50 50
    50 50
    
    Expected output
    10000
    50 200
    100 100