Skip to main content

Checkpoint: Keep the Most

Challenge on lesson: Checkpoint — Restate, Then Sweep

CHALLENGE
Difficulty: advanced+25 XP

**Task.** Each test case gives n intervals [l, r] (integer endpoints, 1 ≤ l < r ≤ 10^9). Remove the minimum number of intervals so that no two remaining intervals share any point (touching at an endpoint counts as sharing: [1,3] and [3,5] overlap at 3). For each test case print one integer: the minimum removals. **Input:** first line T — the number of test cases (1 ≤ T ≤ 8). Each test case: a line with n (1 ≤ n ≤ 200000), then a line with the n pairs l r. Total n over all cases ≤ 200000. **Budget check:** n ≤ 200000 admits O(n log n) — one sort plus one sweep. Any per-pair reasoning beyond the sweep is a wrong family.

Back to lesson: Checkpoint — Restate, Then Sweep