TopCoder problem "CuttingPoles" used in SRM 286 (Division II Level One)



Problem Statement

    

A careless worker planted several poles in a row to build a fence. They all should be the same height, but they were cut at different sizes. The owner wants them not only all at the same height, but also as tall as possible.

Our solution will be to cut the tops of taller poles and "glue" those tops on top of the shorter ones. To do this, we will first sort the poles from tallest to shortest, and proceed as follows:

  • Cut the tip of the tallest pole, leaving its height equal to the average height of the poles (so we do not cut it anymore).
  • "glue" this piece on top of the shortest pole.
  • Re-sort the poles, and continue from the first step until all poles are the same height.

Write a class CuttingPoles with a method countCuts that takes a int[] of pole heights and returns the number of cuts needed to make them all the same height using the algorithm described.

 

Definition

    
Class:CuttingPoles
Method:countCuts
Parameters:int[]
Returns:int
Method signature:int countCuts(int[] poles)
(be sure your method is public)
    
 

Constraints

-poles will contain between 1 and 50 elements, inclusive.
-Each element of poles must be between 1 and 1000, inclusive.
-The average value of poles will be an integer.
 

Examples

0)
    
{1,3}
Returns: 1
Cropping the taller pole to a height of 2 and gluing its top piece onto the shorter pole leaves them both with the same height.
1)
    
{10,10,10}
Returns: 0
No cuts needed.
2)
    
{1,1,3,3}
Returns: 2
3)
    
{10,10,10,10,10,10,10,18}
Returns: 7

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=5905

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=8083&pm=5905

Writer:

ged

Testers:

PabloGilberto , brett1479 , radeye , Olexiy

Problem categories:

Simple Math, Simulation