Convex Hull

Interview

Time limit1sMemory limit128 MB

Summary
Given points already labeled as hull or non-hull, output only the hull points in counterclockwise order starting from the lexicographically smallest one.
Level

Medium4 of 10

Topics
Geometry, Sorting, Implementation, Math
Solved
No attempts yet

Problem

Finding the convex hull of a set of points is a handy skill that comes up often. The task naturally splits into two stages: first, identify which points lie on the convex hull; second, arrange those hull points in counterclockwise order. Assume the first stage is already done — every point is already marked as being on the hull or not. Write a program that performs the second stage: list the hull points in counterclockwise order.

Input

The first line contains the number of points nn (3≤n≤100,0003 \le n \le 100{,}000).

Each of the next nn lines describes one point with three values xx, yy, and cc. Here xx and yy are integers whose absolute value is at most 1,000,000,0001{,}000{,}000{,}000, and cc is the character Y or N: Y means the point lies on the convex hull, and N means it does not.

No two points coincide, and the points are not all collinear.

Output

On the first line, print the number of points that form the convex hull. Then print those points, one x y pair per line, arranged in counterclockwise order. The first point printed must be the one with the smallest xx-coordinate; if several points share that smallest xx-coordinate, choose among them the one with the smallest yy-coordinate.

Examples1

  1. Example 1

    Input
    5
    1 1 Y
    1 -1 Y
    0 0 N
    -1 -1 Y
    -1 1 Y
    
    Expected output
    4
    -1 -1
    1 -1
    1 1
    -1 1