Video Surveillance
Time limit1sMemory limit1024 MB
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 rows and 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 and column is denoted .
The figure below shows a grid with 3 rows, 4 columns, and three images at , , and . 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 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 , , and , the grid size and the number of cells with images (; ). Each of the next lines contains two integers and , the coordinates of a cell with an image (; ). All 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.