Topcoder 250-Point problem from SRM 499, Division 1 (ColorfulRabbits)

Go ahead and think about the problem, and if you think you can solve it, go ahead. Otherwise, here is some help. If a rabbit answers x, then there are x+1 rabbits that have the same color, including the rabbit who answered.

So, the first step (at least for me) is to run through replies and add one to each value. Now, suppose that there are y entries in replies that equal x. If y is less than or equal to x, then all of those rabbits can have the same color: There is a minimum of x rabbits with that color.

Suppose that y is between x+1 and 2x. Then there must be two colors among those rabbits, so the number of rabbits is 2x.

Suppose that y is between 2x+1 and 3x. Then there must be three colors among those rabbits, so the number of rabbits is 3x. And so on.

So what you need to do to solve this problem is to count the number of entries of replies that have each possible value of replies. Then for each of those values, you calculate the minimum number of rabbits. That sum is the minimum number of rabbits.


Let's use example 1. The first thing that we do is increment each element of replies so that it equals { 2, 2, 3, 3 }. Now we count the number of entries that has each value:

Let's take example 2. We first increment each element of replies so that it equals { 3, 3, 45, 3, 3, 3, 445, 3, 3 }. Now we count the number of entries that has each value:

To perform the counting (determining the x and y), I want you to use a map. The keys will be x and the vals will be y. After you initialize the map, you simply run through it figuring out the minimum number of rabbits for each value of x.

There are, of course, other ways to do this. You can sort replies and then do the counting from that. I don't want you do to that, because it's more bug-prone than using a map (and I want you to get practice using the map). You can also insert each value of x into a multiset and use the multiset's count() method. I think that's bug-prone too, and it's inefficient because the multiset is much larger than the map (think about inserting 100 of the same values).