This page is still under construction.

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

Histograms

Time limit1sMemory limit256 MB

Summary
Given histogram H and point set S, build a valid histogram from S points that minimizes diffcount or abserror against H.
Level

Hard8 of 10

Topics
Dynamic programming, Prefix sum
Solved
No attempts yet

Problem

A histogram represents a data distribution. Here a histogram of width WW is given by an even-length sequence of points

(x0,y1),(x1,y1),(x1,y2),(x2,y2),…,(xN/2−1,yN/2),(xN/2,yN/2)(x_0, y_1),(x_1, y_1),(x_1, y_2),(x_2, y_2),\ldots,(x_{N/2-1}, y_{N/2}),(x_{N/2}, y_{N/2})

in the plane. Each adjacent pair shares an xx or a yy coordinate, and horizontal and vertical segments alternate.

The shape must satisfy:

  1. x0=0x_0 = 0
  2. xN/2=Wx_{N/2} = W
  3. xi<xi+1x_i < x_{i+1}
  4. yi>0y_i > 0
  5. yi≠yi+1y_i \ne y_{i+1}

For histogram HH, let yH(x)y_H(x) be the height on unit interval ⟨x,x+1⟩\langle x, x+1 \rangle. Two error measures are used:

diffcount(H′,H)=∑x=0W−1diff(yH(x),yH′(x)),diff(y1,y2)=0 if y1=y2 else 1\text{diffcount}(H', H) = \sum_{x=0}^{W-1} \text{diff}(y_H(x), y_{H'}(x)),\quad \text{diff}(y_1,y_2)=0\text{ if }y_1=y_2\text{ else }1

abserror(H′,H)=∑x=0W−1∣yH(x)−yH′(x)∣\text{abserror}(H', H) = \sum_{x=0}^{W-1} |y_H(x) - y_{H'}(x)|

Given HH, a point set SS, and the measure GG, find a histogram H′H' that uses only points from SS and minimizes the chosen error.

Input

Line 1: integers N,M,GN, M, G (2≤N≤1000002 \le N \le 100000, NN even, 2≤M≤1000002 \le M \le 100000, 1≤G≤21 \le G \le 2). G=1G=1 selects diffcount, G=2G=2 selects abserror.

Next NN lines: points (X,Y)(X, Y) defining histogram HH.

Next MM lines: points (X,Y)(X, Y) in set SS. (0≤X≤1060 \le X \le 10^6, 1≤Y≤1061 \le Y \le 10^6)

Output

Line 1: minimum error DD.

Line 2: even integer LL, the number of points in the optimal histogram.

Next LL lines: coordinates X YX\ Y of those points. The definition must satisfy every rule from the statement.

Examples2

  1. Example 1

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

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