[HDU] [5645] DZY Loves Balls

Problem Description

Loading...

Problem Analysis

A probability problem
a careful derivation is all it takes

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[1000];
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int n;
		scanf("%d",&n);
		for(int i=0; i<n; i++)
			scanf("%d",&a[i]);
		sort(a,a+n);
		double res=0;
		int cnt=0;
		int k=1;
		for(int i=1; i<n; i++) {
			if(a[i]>a[i-1]) {
				cnt+=k;
				k=1;
			} else
				k++;
			res+=(1.0/n)*(cnt*1.0/(n-1));
		}
		printf("%.6lf\n",res);
	}
	return 0;
}

Problem link: HDU 5645 DZY Loves Balls

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...