題目傳送門: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;
}