結果

問題 No.174 カードゲーム(Hard)
ユーザー koyumeishikoyumeishi
提出日時 2015-03-27 02:24:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 849 ms / 2,000 ms
コード長 1,636 bytes
コンパイル時間 935 ms
コンパイル使用メモリ 83,216 KB
実行使用メモリ 11,876 KB
最終ジャッジ日時 2023-09-21 02:50:11
合計ジャッジ時間 8,508 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 843 ms
11,876 KB
testcase_03 AC 844 ms
11,728 KB
testcase_04 AC 845 ms
11,824 KB
testcase_05 AC 844 ms
11,752 KB
testcase_06 AC 845 ms
11,824 KB
testcase_07 AC 849 ms
11,868 KB
testcase_08 AC 847 ms
11,828 KB
testcase_09 AC 845 ms
11,744 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 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){
		//状態を幅優先探索 O(2^n * n)
		vector<double> dp_s(1<<n, 0);
		dp_s[(1<<n) - 1] = 1.0;

		for(int s=(1<<n)-1; s>0; s--){
			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_;
				}
			}
		}

		//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);
	auto prob_b = get_prob(pb);

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

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


	return 0;
}
0