**problem**TopCoder SRM577 DIV1 EllysRoomAssignmentsDiv1

**how to solve**This problem is easy, right?

**source code**

import java.util.Arrays;

public class EllysRoomAssignmentsDiv1 {

public double getAverage(String[] ratings) {

StringBuilder sb = new StringBuilder();

for (int i = 0; i < ratings.length; i++) {

sb.append(ratings[i]);

}

String[] split = sb.toString().split(" ");

int[] rating = new int[split.length];

for (int i = 0; i < rating.length; i++) {

rating[i] = Integer.parseInt(split[i]);

}

return getAverage(rating);

}

private double getAverage(int[] rating) {

int N = rating.length;

int R = N % 20 == 0 ? N / 20 : N / 20 + 1;

if (R == 1) {

return average(rating, 0, N - 1);

}

int elly = rating[0];

Arrays.sort(rating);

int index = Arrays.binarySearch(rating, elly);

double average = 0;

double M = 0;

for (int i = rating.length - 1; i >= 0; i -= R) {

if (i - (R - 1) <= index && index <= i) {

average += elly;

M++;

} else {

if (i - (R - 1) < 0) {

double a = average / M;

double b = (average + average(rating, 0, i)) / (M + 1);

double p = (double) (i - (0 - 1)) / R;

return a * (1d - p) + b * p;

} else {

average += average(rating, i - (R - 1), i);

M++;

}

}

}

return average / M;

}

private double average(int[] rating, int start, int end) {

double sum = 0;

for (int i = start; i <= end; i++) {

sum += rating[i];

}

return sum / ((end + 1) - start);

}

}