This problem is taken from interviewstreet.com
Given array of integers Y=y1,…,yn, we have n line segments such that
endpoints of segment i are (i, 0) and (i, yi). Imagine that from the
top of each segment a horizontal ray is shot to the left, and this ray
stops when it touches another segment or it hits the y-axis. We
construct an array of n integers, v1, …, vn, where vi is equal to
length of ray shot from the top of segment i. We define V(y1, …, yn)
= v1 + … + vn.For example, if we have Y=[3,2,5,3,3,4,1,2], then v1, …, v8 =
[1,1,3,1,1,3,1,2], as shown in the picture below:
For each permutation p of [1,…,n], we can calculate V(yp1, …,
ypn). If we choose a uniformly random permutation p of [1,…,n], what
is the expected value of V(yp1, …, ypn)?Input Format
First line of input contains a single integer T (1 <= T <= 100). T
test cases follow.First line of each test-case is a single integer N (1 <= N <= 50).
Next line contains positive integer numbers y1, …, yN separated by a
single space (0 < yi <= 1000).Output Format
For each test-case output expected value of V(yp1, …, ypn), rounded
to two digits after the decimal point.Sample Input
6 3 1 2 3 3 3 3 3 3 2 2 3 4 10 2 4 4 5 10 10 10 5 10 6 1 2 3 4 5 6Sample Output
4.33 3.00 4.00 6.00 5.80 11.15Explanation
Case 1: We have V(1,2,3) = 1+2+3 = 6, V(1,3,2) = 1+2+1 = 4, V(2,1,3) =
1+1+3 = 5, V(2,3,1) = 1+2+1 = 4, V(3,1,2) = 1+1+2 = 4, V(3,2,1) =
1+1+1 = 3. Average of these values is 4.33.Case 2: No matter what the permutation is, V(yp1, yp2, yp3) = 1+1+1 =
3, so the answer is 3.00.Case 3: V(y1 ,y2 ,y3)=V(y2 ,y1 ,y3) = 5, V(y1, y3, y2)=V(y2, y3, y1) =
4, V(y3, y1, y2)=V(y3, y2, y1) = 3, and average of these values is
4.00.
A naive solution to the problem will run forever for N=50. I believe that the problem can be solved by independently calculating a value for each stick. I still need to know if there is any other efficient approach for this problem. On what basis do we have to independently calculate value for each stick?

As you correctly, noted we can solve problem independently for each stick.
Let F(i, len) is number of permutations, that ray from stick i is exactly len.
Then answer is
All is left is to count F(i, len). Let a(i) be number of sticks j, that y_j<=y_i. b(i) – number of sticks, that b_j>b_i.
In order to get ray of length len, we need to have situation like this.
Where O – is stick #i. B – is stick with bigger length, or beginning. l – is stick with heigth, lesser then ith.
This gives us 2 cases:
1) B is the beginning, this can be achieved in
P(a(i), len-1) * (b(i)+a(i)-(len-1))!ways.2) B is bigger stick, this can be achieved in
P(a(i), len-1)*b(i)*(b(i)+a(i)-len)!*(n-len)ways.edit: corrected b(i) as 2nd term in (mul)in place of a(i) in case 2.