DZY Loves Balls
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 320 Accepted Submission(s): 194
Problem Description
DZY loves playing balls.
He has n 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 A . Next, without putting A back into the box, he randomly picks another ball from the box, and names it B .
If the number written on A is strictly greater than the number on B , he will feel happy.
Now you are given the numbers on each ball. Please calculate the probability that he feels happy.
He has n 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 A . Next, without putting A back into the box, he randomly picks another ball from the box, and names it B .
If the number written on A is strictly greater than the number on B , 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 t
denoting the number of testcases.
t testcases follow. In each testcase, first line contains n , second line contains n space-separated positive integers ai , denoting the numbers on the balls.
(1≤t≤300,2≤n≤300,1≤ai≤300 )
t testcases follow. In each testcase, first line contains n , second line contains n space-separated positive integers ai , denoting the numbers on the balls.
(1≤t≤300,2≤n≤300,1≤ai≤300 )
Output
For each testcase, output a real number with 6 decimal places.
Sample Input
2
3
1 2 3
3
100 100 100
Sample Output
0.500000
0.000000
Source
昨天参加了蓝桥 感觉不是很理想 等出结果吧
补 前天的bc
因为数据规模很小,所以直接用O(n2))时间求出有多少对(i,j)满足ai<aj,然后再除以n(n−1)/2即可。
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 using namespace std; 6 int t; 7 int n; 8 int a[305]; 9 int main() 10 { 11 scanf("%d",&t); 12 for(int i=1;i<=t;i++) 13 { 14 scanf("%d",&n); 15 memset(a,0,sizeof(a)); 16 int jishu=0; 17 for(int j=0;j<n;j++) 18 scanf("%d",&a[j]); 19 sort(a,a+n); 20 for(int j=0;j<n;j++) 21 for(int k=j+1;k<n;k++) 22 { 23 if(a[k]>a[j]) 24 jishu++; 25 } 26 double ans=n*(n-1); 27 printf("%.6f ",jishu/ans); 28 } 29 return 0; 30 }