【每日一题】32. 比赛 (DFS / 概率DP)

补题链接:Here

【方案一:DFS】

首先我们可以计算出每道题做不出来的概率 \(unsolve[i] = (1 - a[i])(1- b[i])(1 - c[i])\)

然后因为只有 12 道题, 每道题要么做对要么做错, 我们可以做 \(DFS\)

当前做对的题数小于 \(need\) 的时候, 我们可以往对和不对的方向搜索

如果做对的题数等于 \(need\) , 那么我们只能往不对的方向搜索

const int N = 110;
double a[N], b[N], c[N];
double unsolve[N];
double ans;
void dfs(int pos, int now, int need, double p) {
	if (pos == 13) {
		if (now == need) ans += p;
		return ;
	}
	if (now < need)dfs(pos + 1, now + 1, need, p * (1 - unsolve[pos]));
	dfs(pos + 1, now , need, p * unsolve[pos]);
}
void solve() {
	for (int i = 1; i <= 12; ++i)cin >> a[i];
	for (int i = 1; i <= 12; ++i)cin >> b[i];
	for (int i = 1; i <= 12; ++i)cin >> c[i];
	for (int i = 1; i <= 12; ++i)
		unsolve[i] = (1 - a[i]) * (1 - b[i]) * (1 - c[i]);
	for (int i = 1; i <= 13; ++i) {
		ans = 0;
		dfs(1, 0, i - 1, 1);
		cout << fixed << setprecision(6) << ans << "\n";
	}
}

【方案二:概率 \(dp\)

这道题还可以用概率 \(dp\) 来做

\(dp[i][j]\) 表示做到第 ii 道题的时候做对 jj 道的状态

显然有 \(dp[i][j] = dp[i - 1][j] * unsolve[i] + dp[i - 1][j - 1] * (1 - unsolve[i])\)

注意初始化 \(dp[0][0] = 1\)

const int N = 105 + 5;
double a[N], b[N], c[N];
double unsolve[N];
double dp[15][15];
void solve() {
	for (int i = 1; i <= 12; ++i)cin >> a[i];
	for (int i = 1; i <= 12; ++i)cin >> b[i];
	for (int i = 1; i <= 12; ++i)cin >> c[i];
	for (int i = 1; i <= 12; ++i)
		unsolve[i] = (1 - a[i]) * (1 - b[i]) * (1 - c[i]);
	dp[0][0] = 1;
	for (int i = 1; i <= 12; ++i) {
		dp[i][0] = dp[i - 1][0] * unsolve[i];
		for (int j = 1; j <= i; ++j)
			dp[i][j] = dp[i - 1][j] * unsolve[i] + dp[i - 1][j - 1] * (1 - unsolve[i]);
	}
	for (int i = 0; i <= 12; ++i)
		cout << fixed << setprecision(6) << dp[12][i] << "\n";
}
posted @ 2021-05-22 13:05  RioTian  阅读(56)  评论(0编辑  收藏  举报