题目传送门:http://www.codeforces.com/contest/626/problem/D
题意:有很多个气球,每个气球上有对应的一个分值,游戏中的每一轮两名玩家任意从中取一个气球,对应分值高的人这一轮获胜。现在进行了三轮游戏,前两轮A玩家获胜,第三轮B玩家获胜,问有多大的概率B玩家三轮的总获得分值比A玩家的高?
解题思路:哈希枚举获胜的分值数,三轮符合题意的情况数除以总情况数即为答案。
#include<cstdio>
#include<algorithm>
using namespace std;
typedef unsigned long long ll;
const int maxn = 2005;
int score[maxn];
ll cnt1[10005];
ll cnt2[10005];
int main()
{
int n;
int cnt = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &score[i]);
sort(score,score+n);
//Winning situations for single round
for (int i = 0; i < n; i++)
{
for (int j = i-1; j >= 0; j--)
{
cnt1[score[i]-score[j]]++;
cnt++;
}
}
//One win a two-streak game situations
for (int i = 1; i <= 5000; i++)
{
for (int j = 1; j <= 5000; j++)
{
cnt2[i+j] += cnt1[i] * cnt1[j];
}
}
double ans = 0.0;
for (int i = 1; i <= 5000; i++)
{
for (int j = i-1; j >= 1; j--)
{
ans += 1.0 * cnt2[j] * cnt1[i] / cnt / cnt / cnt;
}
}
printf("%.10lf\n",ans);
return 0;
}