Wardrobe#1312

Wardrobe (Editorial)

Algorithm Analysis

Let be the length, be the width, be the height of a wardrobe. The wardrobe can be carried through a door opening in one of three ways: from the front (rectangle a \times c must fit into x \times y), from the side (rectangle b \times c must fit into x \times y), or from the top (rectangle a \times b must fit into x \times y).

A rectangle a \times b fits into x \times y if one of the conditions is met:

Algorithm Implementation

Read the input data. Set the variable flag to 0. Iterate through all three possible orientations of the wardrobe in the doorway. If the wardrobe fits through the door in at least one orientation, set flag = 1.

scanf("%d %d %d %d %d", &a, &b, &c, &x, &y);
flag = 0;
if (((a <= x) && (b <= y)) || ((b <= x) && (a <= y))) flag = 1;
if (((b <= x) && (c <= y)) || ((c <= x) && (b <= y))) flag = 1;
if (((a <= x) && (c <= y)) || ((c <= x) && (a <= y))) flag = 1;

Depending on the value of flag, output the corresponding answer.

if (flag == 1)
    printf("YES\n");
else
    printf("NO\n");

Second Solution. The problem can be solved using a single complex condition.

scanf("%d %d %d %d %d", &a, &b, &c, &x, &y);
if (((a <= x) && (b <= y)) || ((b <= x) && (a <= y)) ||
    ((b <= x) && (c <= y)) || ((c <= x) && (b <= y)) ||
    ((a <= x) && (c <= y)) || ((c <= x) && (a <= y)))
    printf("YES\n");
else
    printf("NO\n");

Java Implementation

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner con = new Scanner(System.in);
        int a = con.nextInt();
        int b = con.nextInt();
        int c = con.nextInt();
        int x = con.nextInt();
        int y = con.nextInt();

        int flag = 0;
        if (((a <= x) && (b <= y)) || ((b <= x) && (a <= y))) flag = 1;
        if (((b <= x) && (c <= y)) || ((c <= x) && (b <= y))) flag = 1;
        if (((a <= x) && (c <= y)) || ((c <= x) && (a <= y))) flag = 1;
        if (flag == 1)
            System.out.println("YES");
        else
            System.out.println("NO");

        con.close();
    }
}

Python Implementation

a, b, c, x, y = map(int, input().split())
if (a <= x and b <= y) or (b <= x and a <= y) or (b <= x and c <= y) or (c <= x and b <= y) or (a <= x and c <= y) or (c <= x and a <= y):
    print("YES")
else:
    print("NO")