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,因为有一半是第一个球比第二个球小的情况。
/* ***********************************************
Author :111qqz
Created Time :2016年03月22日 星期二 19时27分40秒
File Name :code/hdu/5645.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=305;
7int n;
8int a[N];
9int cnt[N];
10int main()
11{
12 #ifndef ONLINE_JUDGE
13 freopen("code/in.txt","r",stdin);
14 #endif
15// ios::sync_with_stdio(false);
16 int T;
17 cin>>T;
18 while (T--)
19 {
20 ms(cnt,0);
21 cin>>n;
22 for ( int i = 0 ; i < n ; i++)
23 {
24 cin>>a[i];
25 cnt[a[i]]++;
26 }
27 int cur = 0 ;
28 int fz = 0 ;
29 for ( int i = 1 ; i <= 300 ; i++)
30 {
31 if (cnt[i]==0) continue;
32 fz +=cnt[i]*cur;
33 cur +=cnt[i];
34 //cout<<"fz:"<<fz<<endl;
35 }
1 int fm = n*(n-1)/2;
2 double ans = fz*0.5/(1.0*fm);
3 printf("%.6f\n",ans);
4 }
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}