## DMOPC '15 Contest 2 P6 - Manhattan Magnets

View as PDF

Points: 17 (partial)
Time limit: 2.5s
Memory limit: 512M

Author:
Problem types
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

k_35P likes to play with magnets. He has placed magnets on the floor at integral coordinates, and will soon drop metal pieces. When dropped onto a floor, pieces of this metal will always land at integral coordinates, as if the floor was an infinitely large Cartesian Plane.

Upon reaching the floor, a metal piece will also immediately move to the nearest (as per Manhattan distance) floor tile which has a magnet on it — the magnets do not move. If a metal piece is equidistant to two or more magnets that are tied for closest distance, it will not move, even if the magnets are at the same coordinates.

Before k_35P drops any metal, however, he's allowed you to place exactly one additional magnet at any integral coordinate on the floor (including directly under a tile where a piece of metal will land). You want to place this magnet such that it will collect as many pieces of metal as possible. Unable to solve this problem by hand, you've decided to write a program to determine the maximum number of metal pieces that your magnet can collect, if placed optimally.

#### Note

The Manhattan distance between a given point and another point is defined as .

#### Constraints

For 40% of the points, .

For an additional 10% of the points, .

For all cases, .

#### Input Specification

The first line will contain .

In the next lines, line will contain two space-separated integers and , indicating the coordinates of the -th magnet.

The next line will contain a single integer .

For the next lines, line will contain two space-separated integers and , indicating the coordinates where the -th piece of metal will be dropped.

#### Output Specification

A single integer, the maximum number of metal pieces you can collect by placing a single magnet.

#### Sample Input 1

1
0 0
2
0 1
1 0

#### Sample Output 1

1

#### Explanation for Sample Output 1

A magnet can be placed either directly where the first piece of metal will fall, or the second. Placing it anywhere else will result in it collecting no metal, as both pieces of metal are unit away from the magnet already set up.

#### Sample Input 2

3
0 0
10 0
5 5
3
3 0
5 2
7 0

#### Sample Output 2

3

#### Explanation for Sample Output 2

Place a magnet at , which will be units away from all metal pieces. Since each piece is units away from the nearest magnet, all can be collected by placing it here.

#### Sample Input 3

1
9 21
3
0 14
18 33
21 19

#### Sample Output 3

2

• commented on Nov. 12, 2015, 4:21 p.m. edited

Please note that the memory constraint has been changed.

• commented on Nov. 10, 2015, 6:52 p.m.

Sorry, the magnet must be placed at integer coordinates.

• commented on Nov. 10, 2015, 5:41 p.m.

Can you place the new magnet on top of another magnet?

• commented on Nov. 10, 2015, 5:45 p.m.

Yes.

• commented on Nov. 10, 2015, 5:50 p.m.

Sorry, should have specified: and a piece of metal that is closest to the two magnets on top of each other would not move?

• commented on Nov. 10, 2015, 5:59 p.m.