Under construction

Still being built. Things may change

Video Surveillance

Time limit1sMemory limit1024 MB

Summary
Given k image cells on a grid that wraps around in both directions, find the smallest bounding box reachable by cyclic shifts and the fewest button presses needed to reach it.
Level

Hard8 of 10

Topics
Sorting, Two pointers, Math
Solved
No attempts yet

Problem

The security system of a shopping center is monitored with video cameras. The guard's computer runs a program that shows video streams from several cameras. The screen is a rectangular grid of hh rows and ww columns. Each cell is either empty or shows the image from one camera. The guard can use the buttons left, right, up, and down to move the images.

The left button moves the image in every cell to the cell on its left. The image in the leftmost cell of each row moves to the rightmost cell of that row. The right, up, and down buttons work the same way. The right button moves each image to the cell on its right, and the image in the rightmost cell of each row moves to the leftmost cell of that row. The up button moves each image to the cell above it, and the images in the top row move to the bottom row. The down button moves each image to the cell below it, and the images in the bottom row move to the top row.

Rows are numbered from top to bottom, and columns are numbered from left to right. The cell at row rr and column cc is denoted (r,c)(r, c).

The figure below shows a grid with 3 rows, 4 columns, and three images at (1,1)(1, 1), (2,4)(2, 4), and (3,3)(3, 3). It also shows where these images move after each of the four buttons is pressed.

The guard wants the cells with camera images to be as compact as possible. The compactness of the images is the smallest area of a subrectangle of the grid that contains all displayed images. Pressing the buttons changes the compactness. The left part of the figure below shows an arrangement with compactness 12. Pressing right once and up once changes the compactness to 4.

The grid has kk cells with images. Find the minimum compactness that can be reached using the four buttons, and the minimum number of button presses needed to reach it.

Input

The first line contains three integers hh, ww, and kk, the grid size and the number of cells with images (1≤h,w≤1091 \le h, w \le 10^9; 1≤k≤100 0001 \le k \le 100\,000). Each of the next kk lines contains two integers rir_i and cic_i, the coordinates of a cell with an image (1≤ri≤h1 \le r_i \le h; 1≤ci≤w1 \le c_i \le w). All kk cells are distinct.

Output

Print two integers: the minimum compactness that can be reached using the buttons, and the minimum number of button presses needed to reach that compactness.

Examples2

  1. Example 1

    Input
    1 10 3
    1 5
    1 7
    1 2
    
    Expected output
    6 0
    
  2. Example 2

    Input
    3 4 3
    1 1
    3 4
    1 4
    
    Expected output
    4 2