## No.173 カードゲーム（Medium）

### 実装

#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <cstdio>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
using namespace std;
template <typename Generator>
int choose(double p, vector<int> const & a, vector<bool> & used, int i, Generator & gen) {
int n = a.size();
int k = 0;
if (i != n-1 and uniform_real_distribution<double>()(gen) > p) {
k = uniform_int_distribution<int>(1,n-i-1)(gen);
}
repeat (j,n) if (not used[j]) if (k -- == 0) {
used[j] = true;
return a[j];
}
}
int main() {
random_device device;
default_random_engine gen(device());
int n; double p, q; cin >> n >> p >> q;
vector<int> a(n); repeat (i,n) cin >> a[i]; sort(a.begin(), a.end());
vector<int> b(n); repeat (i,n) cin >> b[i]; sort(b.begin(), b.end());
int win = 0, lose = 0;
repeat (iteration,500000) {
vector<vector<bool> > used(2, vector<bool>(n));
int score = 0;
repeat (i,n) {
int x = choose(p, a, used[0], i, gen);
int y = choose(q, b, used[1], i, gen);
score += (x > y ? 1 : -1) * (x + y);
}
(score > 0 ? win : lose) += 1;
}
printf("%.8lf\n", win /(double) (win + lose));
return 0;
}