This page is still under construction.

Parts of this page are still being built. What you see may change.

Tractor

Time limit1sMemory limit128 MB

Summary
Given up to 50,000 hay bales on a 1000 by 1000 grid, find the fewest bales to remove so an axis-parallel path from the tractor's start to the origin exists.
Level

Hard8 of 10

Topics
Graph, BFS, Binary search, Geometry
Solved
No attempts yet

Problem

After a long day of work, Farmer John forgot that he left his tractor in the middle of the field. His mischievous cows decide to play a prank on him: they drop NN bales of hay (1≤N≤50,0001 \le N \le 50{,}000) at various spots in the field so that Farmer John cannot easily get the tractor out without first removing some of the bales.

The tractor's position and the positions of the NN hay bales are all points in the 2D plane with integer coordinates in the range 11 to 10001000. No hay bale sits on the tractor's starting point. When Farmer John drives the tractor he may only move it parallel to the coordinate axes (north, south, east, west), and each move must be an integer number of units — for example, he might go 22 units north and then 33 units east. The tractor may never move onto a point occupied by a hay bale.

Determine the minimum number of hay bales Farmer John must remove so that he can drive the tractor to the origin (0,0)(0, 0) of the plane.

Input

  • Line 1: Three space-separated integers — the number of bales NN and the tractor's starting coordinates xx and yy.
  • Lines 2 to N+1N+1: Each line gives the coordinates xx and yy of one hay bale.

Output

  • Line 1: The minimum number of hay bales Farmer John must remove so the tractor can reach the origin (0,0)(0, 0).

Examples1

  1. Example 1

    Input
    7 6 3
    6 2
    5 2
    4 3
    2 1
    7 3
    5 4
    6 4
    Expected output
    1