• 【每日一题】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
    你灵魂的欲望,是你命运的先知。

  • 相关阅读:
    dubbo 在不同协议下携带上下文区别
    innodb使用大字段text,blob的一些优化建议(转)
    Redis的过期策略和内存淘汰策略(转)
    在 Docker 里跑 Java,你必须知道的那些事儿!(转)
    如何在宿主机上执行容器里的jmap,jtack,jstat 命令获取信息(原创)
    操作系统实现线程的几种模式 和 java创建线程的3个方式
    MySQL数据库事务各隔离级别加锁情况--read committed && MVCC
    kafka性能调优(转)
    游戏数值系统
    lua函数回调技巧
  • 原文地址:https://www.cnblogs.com/RioTian/p/14798609.html
Copyright © 2020-2023  润新知