Under construction

Still being built. Things may change

LaLa and Magic Circle (LaLa Version)

Time limit2sMemory limit1024 MB

Summary
Repeatedly reflect non-convex chains of a simple polygon until it is convex, then print its vertices counterclockwise from the lexicographically smallest one.
Level

Hard9 of 10

Topics
Geometry, Implementation
Solved
No attempts yet

Problem

LaLa\color{blue}{\text{LaLa}} has a pile of magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circles in her laboratory.

A magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle can be represented as a simple polygon drawn with special ink, and is usable if and only if it is convex. i.e. all of its internal angles are equal or less than π\pi.

LaLa\color{blue}{\text{LaLa}} plans to turn every magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle into a usable one. However, it may lose all its magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}}al power if done incorrectly. Thankfully, LaLa\color{blue}{\text{LaLa}} has the perfect magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}}al tool for that.

The tool works as follows. When you toss in a magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle, if it's usable, it reports that it is. Otherwise, it takes two distinct points uu and vv such that

  • uu and vv lie on the boundary of the convex hull of the magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle, and
  • none of the points on the path from uu to vv through the boundary of the magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle in counterclockwise order lie on the boundary of the convex hull of the magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle, except for uu and vv.

And then it rotates the uu-vv path by π\pi around the midpoint of uu and vv. In other words, for each point ww on the uu-vv path, ww becomes u+v−wu+v-w where the addition is done coordinate-wise over the two dimensional coordinate system over the paper the magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle is drawn on. Note that the result of this modification is also a simple polygon.

LaLa\color{blue}{\text{LaLa}} got annoyed by how long it takes to convert them. In order to finish and take a nap as soon as possible, LaLa\color{blue}{\text{LaLa}} made the following observations.

  1. A magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle always turns into a usable one within a finite number of applications of the tool.
  2. The set of points on the final magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle is independent of the intermediate modifications. In other words, the shape and location of the final magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle is a function of the initial magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle.

Therefore, LaLa\color{blue}{\text{LaLa}} doesn't have to manually turn magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circles into usable ones with the tool. Instead, LaLa\color{blue}{\text{LaLa}} will compute the usable magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle that can be made from the initial magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle by a sequence of modifications by the tool and modify it in one go.

Write a program to help LaLa\color{blue}{\text{LaLa}} compute the final magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle so that she can go take a nap.

Input

The input is given in the following format:

NN

x_0x\_0 y_0y\_0

x_1x\_1 y_1y\_1

⋮\vdots

x_N−1x\_{N-1} y_N−1y\_{N-1}

where the initial magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle is the union of NN line segments connecting points (x_i,y_i)(x\_i, y\_i) and (x_(i+1 mod N),y_(i+1 mod N))(x\_{(i+1 \bmod N)}, y\_{(i+1 \bmod N)}) for all integers 0≤i<N0 \le i < N.

The input satisfies the following constraints:

  • All numbers in the input are integers.
  • 3≤N≤100 0003 \le N \le 100\,000
  • 0≤x_i≤300 0000 \le x\_i \le 300\,000 and 0≤y_i≤300 0000 \le y\_i \le 300\,000 for all integers 0≤i<N0 \le i < N.
  • x_i≠x_jx\_i \ne x\_j or y_i≠y_jy\_i \ne y\_j for all integers 0≤i<j<N0 \le i < j < N.
  • The input defines a counterclockwise traversal of the boundary of a simple polygon. In particular, it does not intersect with itself.

Output

The output should be in the following format:

MM

z_0z\_0 w_0w\_0

z_1z\_1 w_1w\_1

⋮\vdots

z_M−1z\_{M-1} w_M−1w\_{M-1}

where the final usable magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle is the union of MM line segments connecting points (z_i,w_i)(z\_i, w\_i) and (z_(i+1 mod M),w_(i+1 mod M))(z\_{(i+1 \bmod M)}, w\_{(i+1 \bmod M)}) for all integers 0≤i<M0 \le i < M.

The output should satisfy the following constraints:

  • All the numbers in the output are integers.
  • The point (z_0,w_0)(z\_0, w\_0) is lexicographically smaller than the point (z_i,w_i)(z\_i, w\_i) for all integers 1≤i<M1 \le i < M. i.e. (z_0<z_iz\_0 < z\_i) or (z_0=z_iz\_0 = z\_i and w_0<w_iw\_0 < w\_i).
  • Points (z_i,w_i)(z\_i, w\_i), (z_(i+1 mod M),w_(i+1 mod M))(z\_{(i + 1 \bmod M)}, w\_{(i + 1 \bmod M)}), and (z_(i+2 mod M),w_(i+2 mod M))(z\_{(i + 2 \bmod M)}, w\_{(i + 2 \bmod M)}) are not collinear for all integers 0≤i<M0 \le i < M.
  • The output defines a counterclockwise traversal of the boundary of a convex polygon.

It can be proved that the output satisfying the above constraints is unique.

Hint

The following illustrates the initial magic\color{red}{\text{m}} \color{brown}{\text{a}} \color{orange}{\text{g}} \color{blue}{\text{i}} \color{magenta} {\text{c}} circle for the first sample.

The following illustrates the sequence of usage of the tool to make it usable. The dotted part of the boundary is the path modified by the tool, which becomes the red part after the modification.

Step 0Step 1
Step 2Step 3
Step 4

Examples1

  1. Example 1

    Input
    8
    1 0
    4 0
    6 0
    3 1
    5 2
    2 1
    6 3
    0 2
    
    Expected output
    6
    0 2
    1 0
    6 0
    12 3
    9 4
    3 3