COI '13 #2 Histogrami
View as PDFA histogram is a graphical representation of a statistical distribution of data. In other words, it is
a function that assigns a positive integer value to each number in the interval .
For this task, we describe a histogram with a series of points in the standard coordinate system
which, sequentially, determine the top edge of the area that the histogram encloses.
More specifically, we define a histogram of width  with an even number of points in the form:
Therefore, starting from the first point, for every pair of adjacent points it must hold that their
 coordinates are equal and that their 
 coordinates are equal. Thus, the histogram begins and
ends with a horizontal segment and in between horizontal and vertical segments alternate.
Additionally, the following holds for the shape of a histogram:
- the histogram begins on the line
.
- the histogram ends on the line
.
, for each
- the histogram is defined from left to right and the length of each horizontal segment is at least one.
- the height of the histogram is always greater than zero.
- the length of each vertical segment is at least one.
For a histogram , let 
 be the height of the histogram in the interval 
. For example,
the surface area which the histogram encloses can be calculated by the formula 
. If two
histograms, 
 and 
, are given, with equal width 
 (which can possibly be defined with a
different number of points), then we can measure the difference between these two histograms
in various ways. This measure of difference between two histograms is also called inaccuracy of
histogram 
 with regard to histogram 
. In this task, we examine two different ways of measuring
differences between two histograms and we define them in the following way:
In other words, the first way measures the number of unit segments  in which the two
histograms differ, whereas the second way is the sum of absolute values of differences on those
individual unit segments.
Write a program that will, for a given histogram , set of points 
 and way of measuring inaccuracy,
find and output histogram 
 which only uses points from the set 
 in its definition and has
the least possible inaccuracy with regard to the given histogram 
.
The definition of histogram  must comply with all aforementioned conditions (for example, there
shouldn't be two continuous horizontal segments in the definition of histogram 
), and each point
in the definition must be one of the points from the set 
.
Input Specification
The first line of input contains integers  
 even
: the total number of points in the definition of histogram 
, number of points in set 
 and the number which determines what method of measuring the inaccuracy is used: 
 for 
 and 
 for 
.
Each of the following  lines contains integers 
 and 
 
 - coordinates
of one point from the definition of histogram 
. The histogram's definition will comply with all the
conditions from the task statement.
Each of the following  lines contains integers 
 and 
 
 - coordinates
of one point from set 
. All the points in the set 
 will be different from one another, but they
can match with points from the definition of the histogram 
.
Output Specification
The first line of output must contain the least possible inaccuracy .
The second line of output must contain an even integer  - the total number of points in the
definition of the required optimal histogram.
In each of the following  lines, output two integers 
 and 
 - coordinates of the point
in the histogram's definition.
The histogram's definition must comply with all the conditions from the task.
Please note: The solution may not be unique, but the input data will be such that there is always at least one solution.
Constraints
| Subtask | Points | Constraints | 
|---|---|---|
| 1 | 25 | |
| 2 | 25 | |
| 3 | 25 | |
| 4 | 25 | 
Scoring
If the histogram's definition is incorrect or wasn't output, but the first line of output is correct
(minimal inaccuracy), your solution will get  of total points for that test case.
Sample Input 1
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
Sample Output 1
5
6
0 6
3 6
3 3
5 3
5 5
10 5
Sample Input 2
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
Sample Output 2
14
4
0 4
9 4
9 1
10 1
Comments