zoukankan      html  css  js  c++  java
  • 【每日一题】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 << "
    ";
    	}
    }
    

    【方案二:概率 (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] << "
    ";
    }
    

    The desire of his soul is the prophecy of his fate
    你灵魂的欲望,是你命运的先知。

  • 相关阅读:
    unomi漏洞复现
    xxl-job漏洞复现
    cgi漏洞复现
    celery漏洞复现
    bash漏洞复现
    学习ASP.NET的一些学习资源
    用EF DataBase First做一个简单的MVC3报名页面
    怎样在Word中插入代码并保持代码原始样式不变
    安装notepad++之后怎样在鼠标右键上加上Edit with notepad++
    安装Visual Studio 2010之后怎样安装MSDN Library
  • 原文地址:https://www.cnblogs.com/RioTian/p/14798609.html
Copyright © 2011-2022 走看看