G. Repair

Codeforces
IDCF10097G
Time2000ms
Memory256MB
Difficulty
English · Original
Formal · Original
Alex is repairing his country house. He has a rectangular metal sheet of size a × b. He has to cut two rectangular sheets of sizes a1 × b1 and a2 × b2 from it. All cuts must be parallel to the sides of the initial sheet. Determine if he can do it. The first line contains two space-separated integers a and b (1 ≤ a, b ≤ 109) — the sizes of the initial sheet. The second line contains two space-separated integers a1 and b1 (1 ≤ a1, b1 ≤ 109) — the sizes of the first sheet to cut out. The third line contains two space-separated integers a2 and b2 (1 ≤ a2, b2 ≤ 109) — the sizes of the second sheet to cut out. Output «_YES_» (without quotes), if it's possible to cut two described sheets from the initial sheet, or «_NO_» (without quotes), if it's not possible. ## Input The first line contains two space-separated integers a and b (1 ≤ a, b ≤ 109) — the sizes of the initial sheet.The second line contains two space-separated integers a1 and b1 (1 ≤ a1, b1 ≤ 109) — the sizes of the first sheet to cut out.The third line contains two space-separated integers a2 and b2 (1 ≤ a2, b2 ≤ 109) — the sizes of the second sheet to cut out. ## Output Output «_YES_» (without quotes), if it's possible to cut two described sheets from the initial sheet, or «_NO_» (without quotes), if it's not possible. [samples]
**Definitions** Let $ a, b \in \mathbb{Z}^+ $ be the dimensions of the initial rectangular sheet. Let $ (a_1, b_1), (a_2, b_2) \in \mathbb{Z}^+ \times \mathbb{Z}^+ $ be the dimensions of the two target rectangles. For each target rectangle, rotation is allowed: i.e., $ (a_i, b_i) $ may be used as $ (b_i, a_i) $. **Constraints** 1. $ 1 \leq a, b \leq 10^9 $ 2. $ 1 \leq a_1, b_1, a_2, b_2 \leq 10^9 $ **Objective** Determine whether there exist orientations $ o_1, o_2 \in \{0, 1\} $ such that the two rectangles $ R_1 = (a_1^{o_1}, b_1^{o_1}) $ and $ R_2 = (a_2^{o_2}, b_2^{o_2}) $, where $$ x^0 = x, \quad x^1 = y, \quad y^0 = y, \quad y^1 = x $$ can be placed non-overlappingly and entirely within the $ a \times b $ rectangle, with sides parallel to the axes. That is, determine if there exists a way to partition the $ a \times b $ rectangle (via axis-aligned placement) to contain both $ R_1 $ and $ R_2 $ without overlap. **Feasibility Conditions** Define the set of possible orientations: - $ O_1 = \{ (a_1, b_1), (b_1, a_1) \} $ - $ O_2 = \{ (a_2, b_2), (b_2, a_2) \} $ Then, output "YES" if there exist $ (w_1, h_1) \in O_1 $, $ (w_2, h_2) \in O_2 $, such that **at least one** of the following holds: 1. **Side-by-side horizontally**: $ w_1 + w_2 \leq a $ and $ \max(h_1, h_2) \leq b $ 2. **Stacked vertically**: $ h_1 + h_2 \leq b $ and $ \max(w_1, w_2) \leq a $ 3. **L-shaped (w1 + w2 ≤ a, h1 + h2 ≤ b, with one placed beside and above the other)**: $ w_1 + w_2 \leq a $ and $ h_1 + h_2 \leq b $ and $ w_1 \leq a $, $ w_2 \leq a $, $ h_1 \leq b $, $ h_2 \leq b $ *(Note: This is implied by the above two if we allow arbitrary placement; however, in axis-aligned non-overlapping packing of two rectangles into a bounding rectangle, the only viable configurations are side-by-side, stacked, or one placed beside and above — which reduces to checking the two above plus two more:)* Actually, to be complete, also check: 4. $ w_1 + h_2 \leq a $ and $ h_1 + w_2 \leq b $ (one rotated to fit beside and above) 5. $ w_2 + h_1 \leq a $ and $ h_2 + w_1 \leq b $ But the standard and sufficient approach is to check all 4 combinations of orientations, and for each, check: - Horizontal arrangement: $ w_1 + w_2 \leq a $ and $ \max(h_1, h_2) \leq b $ - Vertical arrangement: $ h_1 + h_2 \leq b $ and $ \max(w_1, w_2) \leq a $ - “L”-shaped: $ w_1 + w_2 \leq a $ and $ h_1 + h_2 \leq b $ — **but this is NOT sufficient alone** Actually, the **correct and complete set** of configurations to check for two rectangles in a bounding rectangle is: For each $ (w_1, h_1) \in O_1 $, $ (w_2, h_2) \in O_2 $: - **Side-by-side**: $ w_1 + w_2 \leq a $ and $ h_1 \leq b $ and $ h_2 \leq b $ and $ \max(h_1, h_2) \leq b $ - **Stacked**: $ h_1 + h_2 \leq b $ and $ w_1 \leq a $ and $ w_2 \leq a $ and $ \max(w_1, w_2) \leq a $ - **One beside, one above (L-shape)**: - $ w_1 + w_2 \leq a $ and $ h_1 + h_2 \leq b $ - **AND** $ w_1 \leq a $, $ w_2 \leq a $, $ h_1 \leq b $, $ h_2 \leq b $ — always true if above holds But the L-shape requires: Either: - $ w_1 + w_2 \leq a $ and $ h_1 + h_2 \leq b $ (This allows placing one rectangle to the right of the other, and one above the other — but only if they are arranged in an L, meaning one is placed in corner, the other in adjacent corner — which is valid if the bounding box is $ \max(w_1, w_2) \times \max(h_1, h_2) $, which is **not** the case) Actually, **the only two valid configurations** are: - **Horizontal**: total width $ w_1 + w_2 \leq a $, height $ \max(h_1, h_2) \leq b $ - **Vertical**: total height $ h_1 + h_2 \leq b $, width $ \max(w_1, w_2) \leq a $ - **Mixed (L-shape)**: - $ w_1 + h_2 \leq a $ and $ h_1 + w_2 \leq b $ - $ w_2 + h_1 \leq a $ and $ h_2 + w_1 \leq b $ So in total, for each of the 4 orientation pairs, check: 1. $ w_1 + w_2 \leq a $ and $ \max(h_1, h_2) \leq b $ 2. $ h_1 + h_2 \leq b $ and $ \max(w_1, w_2) \leq a $ 3. $ w_1 + h_2 \leq a $ and $ h_1 + w_2 \leq b $ 4. $ w_2 + h_1 \leq a $ and $ h_2 + w_1 \leq b $ But (3) and (4) are redundant if we consider all 4 orientation combinations — since $ (w_1, h_1) $ and $ (w_2, h_2) $ already include both orientations. Thus, simply iterate over all 4 combinations of orientations, and for each, check: - $ w_1 + w_2 \leq a $ and $ \max(h_1, h_2) \leq b $ - $ h_1 + h_2 \leq b $ and $ \max(w_1, w_2) \leq a $ **Final Objective** $$ \exists \, (w_1, h_1) \in \{(a_1, b_1), (b_1, a_1)\}, \quad (w_2, h_2) \in \{(a_2, b_2), (b_2, a_2)\} $$ such that $$ (w_1 + w_2 \leq a \land \max(h_1, h_2) \leq b) \quad \lor \quad (h_1 + h_2 \leq b \land \max(w_1, w_2) \leq a) $$ Output "YES" if true, "NO" otherwise.
API Response (JSON)
{
  "problem": {
    "name": "G. Repair",
    "description": {
      "content": "Alex is repairing his country house. He has a rectangular metal sheet of size a × b. He has to cut two rectangular sheets of sizes a1 × b1 and a2 × b2 from it. All cuts must be parallel to the sides o",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10097G"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Alex is repairing his country house. He has a rectangular metal sheet of size a × b. He has to cut two rectangular sheets of sizes a1 × b1 and a2 × b2 from it. All cuts must be parallel to the sides o...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ a, b \\in \\mathbb{Z}^+ $ be the dimensions of the initial rectangular sheet.  \nLet $ (a_1, b_1), (a_2, b_2) \\in \\mathbb{Z}^+ \\times \\mathbb{Z}^+ $ be the dimensions of the two t...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments