And Then, How Many Are There?
Time limit5sMemory limit128 MB
Given stacked discs of four colors, repeatedly remove two same-colored discs that are both uncovered; find the maximum number of discs removable.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Bit manipulation, Geometry, Sorting
- Solved
- No attempts yet
Problem
Mr. Solitarius, a famous creator of solo-play games, comes up with a new idea almost every day. His latest game uses discs of various colors and sizes.
At the start, all the discs are randomly scattered around the center of a table. During play you may remove a pair of discs of the same color, provided that neither of them has any disc lying on top of it. Note that a disc is not considered to lie on top of another when the two are only externally tangent.

For example, in the figure above you can first remove the two black discs; doing so then makes it possible to remove the two white discs. The two gray discs, on the other hand, can never be removed.
Given the colors, sizes, and initial positions of the discs, compute the maximum number of discs that can be removed.
Input
The input consists of several datasets. Each dataset describes the state of a game immediately after all the discs have been scattered, in the following format:
n
x1 y1 r1 c1
x2 y2 r2 c2
...
xn yn rn cn
The first line contains a positive integer , the number of discs. Each of the next lines contains four space-separated integers describing one disc:
- is the center, the radius, and the color index of the -th disc.
- Whenever the -th disc lies on top of the -th disc, holds.
Every color index is an integer between and inclusive, and at most discs in a dataset share the same color. Every center coordinate is between and inclusive, and every radius is between and inclusive.
The end of the input is indicated by a line containing a single zero.
Output
For each dataset, print a single line containing one integer: the maximum number of discs that can be removed.