hdu 5645 DZY Loves Balls (古典概型)

题目链接 题意:n(n<=300)个球,每个球上标有一个标号(a[i]<=300),从中拿一个,不放回,再拿一个,问第一个球上的数字严格大于第二个球上的数字的概率。 思路:古典概型。总数为n*(n-1)/2…然后标号最大300,不妨用cnt[i]统计标号为i的球的个数。从小往大扫一遍cnt,cnt[i]对分子的贡献就是cnt[i]*cur。。cur 为 sum{cnt[1]..cnt[i-1]}; 最后注意将分子除以2,因为有一半是第一个球比第二个球小的情况。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年03月22日 星期二 19时27分40秒
 4File Name :code/hdu/5645.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=305;
34int n;
35int a[N];
36int cnt[N];
37int main()
38{
39	#ifndef  ONLINE_JUDGE 
40	freopen("code/in.txt","r",stdin);
41  #endif
42//	ios::sync_with_stdio(false);
43	int T;
44	cin>>T;
45	while (T--)
46	{
47	    ms(cnt,0);
48	    cin>>n;
49	    for ( int i = 0 ; i < n ; i++)
50	    {
51		cin>>a[i];
52		cnt[a[i]]++;
53	    }
54	    int cur = 0 ;
55	    int fz = 0 ;
56	    for ( int i = 1 ; i <= 300 ; i++)
57	    {
58		if (cnt[i]==0) continue;
59		fz +=cnt[i]*cur;
60		cur +=cnt[i];
61		//cout<<"fz:"<<fz<<endl;
62	    }
63
64	    int fm = n*(n-1)/2;
65	    double ans = fz*0.5/(1.0*fm);
66	    printf("%.6f\n",ans);
67    	}
68
69  #ifndef ONLINE_JUDGE  
70  fclose(stdin);
71  #endif
72    return 0;
73}