This page is still under construction.

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

Broken Line 07

Time limit0.1sMemory limit512 MB

Summary
Construct a rectilinear broken line from the origin that passes through all given dots, minimizing the number of segments; this is an output-only optimization task.
Level

Medium7 of 10

Topics
Sorting, Greedy, Implementation
Solved
No attempts yet

Problem

Azerbaijan is famous for its carpets. As a master carpet designer, you want to make a new design by drawing a broken line. A broken line is a sequence of tt line segments in a two-dimensional plane, defined by a sequence of t+1t+1 points p0,…,ptp_0, \ldots, p_t as follows. For each 0≤j≤t−10 \leq j \leq t-1 there is a segment connecting points pjp_j and pj+1p_{j+1}.

To make the new design, you have already marked nn dots in a two-dimensional plane. The coordinates of dot ii (1≤i≤n1 \leq i \leq n) are (x[i],y[i])(x[i], y[i]). No two dots have the same x or the same y coordinate.

You now want to find a sequence of points (sx[0],sy[0]),(sx[1],sy[1]),…,(sx[k],sy[k])(sx[0], sy[0]), (sx[1], sy[1]), \ldots, (sx[k], sy[k]) that defines a broken line which

  • starts at (0,0)(0, 0) (that is, sx[0]=0sx[0] = 0 and sy[0]=0sy[0] = 0),
  • contains all of the dots (not necessarily as the endpoints of the segments), and
  • consists solely of horizontal or vertical segments (two consecutive points defining the broken line have an equal x or y coordinate).

The broken line is allowed to intersect or overlap itself in any way. Formally, each point of the plane may belong to any number of segments of the broken line.

This is an output-only task with partial scoring. You are given 1010 input files specifying the locations of dots. For each input file, you should submit an output file describing a broken line with the required properties. For each output file that describes a valid broken line, your score depends on the number of segments in the broken line (see Scoring below).

Input

Each input file is in the following format:

  • line 11:     n\;\;n
  • line 1+i1+i (for 1≤i≤n1 \leq i \leq n):     x[i]    y[i]\;\; x[i] \;\; y[i]

Output

Each output file must be in the following format:

  • line 11:     k\;\;k
  • line 1+j1+j (for 1≤j≤k1 \leq j \leq k):     sx[j]    sy[j]\;\; sx[j] \;\; sy[j]

Note that the second line should contain sx[1]sx[1] and sy[1]sy[1] (i.e., the output should not contain sx[0]sx[0] and sy[0]sy[0]). Each sx[j]sx[j] and sy[j]sy[j] should be an integer.

Constraints

  • 1≤n≤100 0001 \leq n \leq 100\,000
  • 1≤x[i],y[i]≤1091 \leq x[i], y[i] \leq {10}^9
  • All values of x[i]x[i] and y[i]y[i] are integers.
  • No two dots have the same xx or the same yy coordinate, i.e. x[i1]≠x[i2]x[i_1] \neq x[i_2] and y[i1]≠y[i2]y[i_1] \neq y[i_2] for i1≠i2i_1 \neq i_2.
  • −2⋅109≤sx[j],sy[j]≤2⋅109-2 \cdot {10}^9 \leq sx[j], sy[j] \leq 2 \cdot {10}^9

Examples1

  1. Example 1

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