TopCoder problem "CrazyLine" used in SRM 486 (Division II Level Three)



Problem Statement

    

A rigorous teacher makes all his students stand in a line before entering the classroom. Being a rigorous teacher, he makes the students line up in non-descending order by height. One time, while the students were lining up, the teacher had to go to the bathroom. The students took the opportunity to play with the teacher's head and make a crazy line. They defined the craziness of a line as the sum of the absolute difference in height between each pair of consecutive students. Since a line ordered by height has minimum craziness, they of course decided to arrange themselves in a line with maximum craziness.

You are given a int[] heights, where each element is the height of a single student. Return the maximum possible craziness of a line made by those students.

 

Definition

    
Class:CrazyLine
Method:maxCrazyness
Parameters:int[]
Returns:int
Method signature:int maxCrazyness(int[] heights)
(be sure your method is public)
    
 

Constraints

-heights will contain between 1 and 50 elements, inclusive.
-Each element of heights will be between 1 and 1000, inclusive.
 

Examples

0)
    
{5,10,25,40,25}
Returns: 100
If the line is arranged in this order: 25-10-40-5-25, the sum of the differences is 15+30+35+20=100.
1)
    
{2,3,5,7,11,13,17,19}
Returns: 82
The following order is optimal: 7-17-5-13-2-19-3-11.
2)
    
{1,1,1,1,1,1,501}
Returns: 1000
Make sure to place the tall guy in the middle.
3)
    
{1000,1000,1000,1000,1000,1000,1000,1000,1000}
Returns: 0
There is not much difference.

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=14239&pm=10926

Writer:

soul-net

Testers:

PabloGilberto , battyone , ivan_metelsky

Problem categories:

Greedy, Math, Sorting