結果

問題 No.174 カードゲーム(Hard)
ユーザー koyumeishikoyumeishi
提出日時 2015-03-27 01:58:52
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,236 ms / 2,000 ms
コード長 1,994 bytes
コンパイル時間 846 ms
コンパイル使用メモリ 88,360 KB
実行使用メモリ 20,244 KB
最終ジャッジ日時 2023-09-21 02:50:49
合計ジャッジ時間 11,383 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1,236 ms
20,244 KB
testcase_03 AC 1,209 ms
20,092 KB
testcase_04 AC 1,213 ms
20,188 KB
testcase_05 AC 1,209 ms
20,072 KB
testcase_06 AC 1,215 ms
20,096 KB
testcase_07 AC 1,223 ms
20,088 KB
testcase_08 AC 1,236 ms
20,228 KB
testcase_09 AC 1,229 ms
20,076 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
#include <ctime>
using namespace std;

int bit_count(int s){
	int ret = 0;
	while(s>0){
		ret++;
		s -= s&-s;
	}
	return ret;
}

int main(){
	int n;
	double pa,pb;
	cin >> n >> pa >> pb;

	vector<int> a(n),b(n);
	for(int i=0; i<n; i++){
		cin >> a[i];
	}
	sort(a.begin(), a.end());

	for(int i=0; i<n; i++){
		cin >> b[i];
	}
	sort(b.begin(), b.end());

	auto get_prob = [&](double p){
		vector<double> dp_s(1<<n, 0);
		vector<bool> used(1<<n, false);
		dp_s[(1<<n) - 1] = 1.0;

		queue<int> q;
		q.push((1<<n) - 1);
		while(q.size() > 0){
			int s = q.front(); q.pop();
			if(used[s]) continue;
			used[s] = true;

			for(int i=0; i<n; i++){
				if((s>>i)&1){
					double p_;
					if(bit_count(s) == 1) p_ = 1.0;
					else if( (1<<i) == (s&-s) ) p_ = p;
					else p_ = (1.0-p)/(bit_count(s)-1);

					dp_s[ s & ~(1<<i) ] += dp_s[s] * p_;

					q.push(s & ~(1<<i));
				}
			}
		}

		//dp[k][x] := 残りk枚の時カードxを出す確率
		vector<vector<double>> dp(n+1, vector<double>(n, 0));
		for(int i=0; i<(1<<n); i++){
			int cnt = bit_count(i);
			for(int k=0; k<n; k++){
				if((i>>k)&1){
					double p_;
					if(bit_count(i) == 1) p_ = 1.0;
					else if( (1<<k) == (i&-i) ) p_ = p;
					else p_ = (1.0-p)/(bit_count(i)-1);
					dp[cnt][k] += dp_s[i]*p_;
				}
			}
		}

		return dp;
	};

	auto prob_a = get_prob(pa);
	/*
	for(int i=0; i<n+1; i++){
		for(int j=0; j<n; j++){
			cerr << prob_a[i][j] << " ";
		}
		cerr << endl;
	}
	*/
	auto prob_b = get_prob(pb);
	/*
	for(int i=0; i<n+1; i++){
		for(int j=0; j<n; j++){
			cerr << prob_b[i][j] << " ";
		}
		cerr << endl;
	}
	*/

	double e = 0;
	for(int k=0; k<=n; k++){
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				if(a[i] < b[j]) continue;
				e += (a[i]+b[j]) * prob_a[k][i] * prob_b[k][j];
			}
		}
	}

	printf("%.16f\n", e);


	return 0;
}
0