結果

問題 No.174 カードゲーム(Hard)
ユーザー kazumakazuma
提出日時 2018-12-28 13:40:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 323 ms / 2,000 ms
コード長 1,674 bytes
コンパイル時間 2,444 ms
コンパイル使用メモリ 205,740 KB
実行使用メモリ 20,264 KB
最終ジャッジ日時 2024-04-09 03:12:00
合計ジャッジ時間 5,739 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,948 KB
testcase_02 AC 323 ms
20,228 KB
testcase_03 AC 321 ms
20,264 KB
testcase_04 AC 321 ms
20,092 KB
testcase_05 AC 322 ms
20,116 KB
testcase_06 AC 316 ms
20,104 KB
testcase_07 AC 323 ms
20,244 KB
testcase_08 AC 318 ms
20,104 KB
testcase_09 AC 317 ms
20,140 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ld = long double;

vector<vector<ld>> calc(int N, ld P) {
	vector<ld> dp(1 << N);
	vector<vector<ld>> p(N, vector<ld>(N));
	dp[0] = 1;
	for (int i = 0; i < (1 << N) - 1; i++) {
		int id = 0, cnt = __builtin_popcount(i);
		for (int j = 0; j < N; j++) if (((i >> j) & 1) == 0) {
			id = j;
			break;
		}
		if (cnt + 1 == N) {
			p[cnt][id] += dp[i];
			dp[i | (1 << id)] += dp[i];
			continue;
		}
		p[cnt][id] += P * dp[i];
		dp[i | (1 << id)] += P * dp[i];
		ld pp = (1 - P) / (N - (cnt + 1));
		for (int j = 0; j < N; j++) if (((i >> j) & 1) == 0 && j != id) {
			p[cnt][j] += pp * dp[i];
			dp[i | (1 << j)] += pp * dp[i];
		}
	}
	return p;
}

int main()
{
	cout << fixed << setprecision(10);
	int N;
	ld Pa, Pb;
	cin >> N >> Pa >> Pb;
	vector<int> A(N), B(N);
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	for (int i = 0; i < N; i++) {
		cin >> B[i];
	}
	sort(A.begin(), A.end());
	sort(B.begin(), B.end());
	auto p = calc(N, Pb);
	vector<vector<ld>> bsum(N, vector<ld>(N + 1));
	vector<vector<ld>> psum(N, vector<ld>(N + 1));
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			bsum[i][j + 1] = bsum[i][j] + p[i][j] * B[j];
			psum[i][j + 1] = psum[i][j] + p[i][j];
		}
	}
	vector<ld> dp(1 << N);
	for (int i = 1; i < (1 << N); i++) {
		int cnt = N - __builtin_popcount(i);
		ld p1 = cnt == N - 1 ? 1 : Pa, p2 = cnt == N - 1 ? 0 : (1 - Pa) / (N - 1 - cnt);
		for (int j = 0, k = 0, c = 0; j < N; j++) if (i & (1 << j)) {
			while (k < N && B[k] < A[j]) ++k;
			dp[i] += (dp[i ^ (1 << j)] + bsum[cnt][k] + A[j] * psum[cnt][k]) * (!c ? p1 : p2);
			++c;
		}
	}
	cout << dp.back() << endl;
	return 0;
}
0