Connection Status:
Competition Arena > CuttingPoles
SRM 286 · 2006-01-30 · by ged · Simple Math, Simulation
Class Name: CuttingPoles
Return Type: int
Method Name: countCuts
Arg Types: (vector<int>)
Problem Statement

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.

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
4)
{10,1,9,2,8,3,7,4,6,10}
Returns: 5

Submissions are judged against all 89 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class CuttingPoles with a public method int countCuts(vector<int> poles) · 89 test cases · 2 s / 256 MB per case

Submitting as anonymous