Surveillance Cameras

Interview

Time limit1sMemory limit128 MB

Summary
Given up to 50,000 distinct grid points, decide whether three axis-parallel lines (full rows or columns) can cover all of them.
Level

Medium7 of 10

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

Problem

Changyoung wants to monitor all of his NN cows (1≤N≤50,0001 \le N \le 50{,}000) using three newly purchased surveillance cameras.

The ii-th cow is located at (xi,yi)(x_i, y_i), where xix_i and yiy_i are integers with 0≤xi,yi≤1,000,000,0000 \le x_i, y_i \le 1{,}000{,}000{,}000. No two cows share the same coordinates.

Each surveillance camera monitors every cow that lies on a single vertical line or a single horizontal line. In other words, one camera covers an entire column x=ax = a or an entire row y=by = b.

Write a program that determines whether all cows can be monitored using three cameras. Equivalently, decide whether the NN points in the plane can all be covered by three axis-parallel lines.

Input

The first line contains the number of cows NN.

Each of the next NN lines contains the coordinates xix_i and yiy_i of a cow, separated by a space.

Output

Print 11 if all cows can be monitored with three surveillance cameras, and 00 otherwise.

Hint

Suppose there are 66 cows located at (1,7)(1,7), (0,0)(0,0), (1,2)(1,2), (2,0)(2,0), (1,4)(1,4), and (3,4)(3,4). Placing the cameras at y=0y = 0, x=1x = 1, and y=4y = 4 monitors every cow, so the answer is 11.

Examples1

  1. Example 1

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