結果

問題 No.174 カードゲーム(Hard)
コンテスト
ユーザー koyumeishi
提出日時 2015-03-27 02:19:08
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 884 ms / 2,000 ms
コード長 1,969 bytes
コンパイル時間 860 ms
コンパイル使用メモリ 88,568 KB
実行使用メモリ 13,112 KB
最終ジャッジ日時 2024-07-06 21:33:04
合計ジャッジ時間 8,342 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

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);
		vector<bool> used(1<<n, false);
		vector<bool> in_queue(1<<n, false);
		dp_s[(1<<n) - 1] = 1.0;
		queue<int> q;
		q.push((1<<n) - 1);
		in_queue[(1<<n) - 1] = true;
		while(q.size() > 0){
			int s = q.front(); q.pop();
			if(used[s]) continue;
			used[s] = true;
			in_queue[s] = false;

			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_;

					if(in_queue[s & ~(1<<i)]) continue;
					q.push(s & ~(1<<i));
					in_queue[s & ~(1<<i)] = true;
				}
			}
		}

		//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