IITD5 - Expected Cycle Sums

We are given a sequence S of N distinct integers. Denote by S[i] as ith element of S.
Hardik picks up a random permutation of S , breaks it into product of disjoint cycles & looks at cycle containing S[i].He notes down the sum of all element of this cycle. Call the expected value of this sum as cycleSum[i]. Your task is to find the minimum value amongst all cycleSums.

Assume all permutations of these N numbers are equally likely.


Input Format :

First line contains an integer T which denotes the number of test cases. Then follow description of T test scenarios. Each test scenario takes 2 lines. First line contains a single integer N, the size of S. Then follows second line containing N elements of S.


Output Format :


Print answer for each test case , rounded to exactly one decimal place , in one line each.

Sample Input :
2
1
1
2
1 2


Sample Output:
1.0
2.0


Note: Notion of cycles for any sequence is defined by using index in the sequence (1-N).

Explaination for sample output :

In first case only possible permutation is (1)  So answer is trivially 1.0

In second case possible permutations are (1)(2) & (12).  As both of these are equally likely, cycleSum[1] = 1/2 * 1 + 1/2 * (1+2) = 2.0

And cycleSum[2] = 1/2 * 2 + 1/2 * (1+2) = 2.5.  Smaller of these is 2.0 , hence the answer.

Constraints :

1<=T<=500
1<=N<=5000
All elements of S are distinct integers in range 0 to 10^5


Added by:Nikhil Garg
Date:2010-10-15
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 NODEJS OBJC VB.NET
Resource:own problem, used for IIT Delhi ACM ICPC provincial contest 2010

hide comments
2014-07-28 23:22:34 Saurabh Jain
How the possible permutations are (1),(2) or (12) shouldn't it be 1,2 and 2,1
2010-10-23 17:24:56 Nikhil Garg
@TheChamp - No where in the problem it is written that permutation would be broken in only 2 disjoint cycles.
2010-10-23 17:22:16 The Champ
the test cases leave an ambiguity, will that permutation of S be broken into disjoints of only two sets. for example, s=1,2,3,4 so is (1)(2)(3,4) allowed since there are three disjoint sets
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.