DZY Loves Balls

DZY Loves Balls

Accepts: 662
Submissions: 1392
Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 262144/262144 K (Java/Others)
Problem Description

DZY loves playing balls.

He has nnn balls in a big box. On each ball there is an integer written.

One day he decides to pick two balls from the box. First he randomly picks a ball from the box, and names it AAA. Next, without putting AAA back into the box, he randomly picks another ball from the box

 

, and names it BBB.

If the number written on AAA is strictly greater than the number on BBB, he will feel happy.

Now you are given the numbers on each ball. Please calculate the probability that he feels happy.

Input

First line contains ttt denoting the number of testcases.

ttt testcases follow. In each testcase, first line contains nnn, second line contains nnn space-separated positive integers aia_iai, denoting the numbers on the balls.

(1≤t≤300,2≤n≤300,1≤ai≤3001\le t\le 300, 2\le n \le 300,1\le a_i \le 3001t300,2n300,1ai300)

Output

For each testcase, output a real number with 6 decimal places.

Sample Input
Copy
2
3
1 2 3
3
100 100 100
Sample Output
Copy
0.500000
0.000000
 
    
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[1000];
int main()
{
    int t,n,i,j;
    double b;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        double p;
        p=n*(n-1);
        sort(a,a+n);
        double sum=0;
        for(i=n-1;i>=0;i--)
        {
            for(j=i-1;j>=0;j--)
            {
                if(a[i]>a[j])
                {
                    sum=sum+1;
                }
            }
        }
      b=sum/p;
      printf("%.6lf\n",b);
    }
}

你可能感兴趣的:(DZY Loves Balls)