zoukankan      html  css  js  c++  java
  • 2021牛客暑期多校训练营5 B. Boxes(概率期望)

    链接:https://ac.nowcoder.com/acm/contest/11256/B
    来源:牛客网

    题目描述

    There're nn boxes in front of you. You know that each box contains a ball either in white or in black. The probability for a ball to be white is 1221, and the colors of balls are independent of each other. The PJ King invites you to guess the colors of all balls. PJ King has assigned some costs to the boxes. If we number the boxes from 11 to nn, the cost to open the box ii is wiwi, and after a box is opened you can see the ball inside this box.

    For sure, there's no way to know all the colors except by opening all boxes. However, Gromah wants to give you some hints. Gromah can tell you secretly the number of black balls among all boxes that have not been opened yet, but you have to pay CC cost to get one such hint from Gromah. Anyway, if you're superpowered, you can do it without any hint. What's the mathematical expectation of the minimum cost to figure out all colors of balls?

    输入描述:

    The first line contains an integer n (1≤n≤105)n (1≤n≤105) and a decimal C (0<C≤109)C (0<C≤109), representing the number of boxes and the cost to get a hint from Gromah.
    
    The second line contains nn decimals w1,w2,⋯ ,wn (0<wi≤109)w1,w2,⋯,wn (0<wi≤109).
    
    All decimal numbers in the input have at most six decimal places.
    

    输出描述:

    Output one line with the expected minimum cost. Your answer will be considered to be correct if the relative or absolute error is less than 10−610−6.
    

    示例1

    输入

    复制

    2 0.1
    1 1
    

    输出

    复制

    0.6
    

    说明

    For the first test case, you can pay 0.10.1 cost to get a hint from Gromah. If the number of black balls is 00 or 22, you will know the colors in each box. This case has a probability of 1221. Otherwise, you will know that the colors of the two balls are distinct, so you only have to open any of the boxes. Therefore, the expected cost is 0.1+12×1=0.60.1+21×1=0.6.
    

    示例2

    输入

    复制

    4 0.123456
    1 1 1 1
    

    输出

    复制

    2.248456
    

    如果不用hint的话答案就是对所有的w求和。如果用hint的话其实也只需要用一次。因为只要一开始用了,后面每开一次箱就能根据开箱结果知道新的黑球的数目(开出来的是黑球那么黑球数量--,如果是白球那么黑球数量不变)。因为题目要求最小化期望,所以先对w进行排序,按照w由小到大的顺序开箱(因为每个箱子概率完全一样且相互独立,如果先开小的开到某个箱子可以根据推出来的黑球的数量直接判断后面箱子里球的颜色就不用继续开了。这样算出来的答案是(C + Sigma_{i = 1}^{n - 1}sum[i] imes frac{1}{2}^{n - i}),其中(sum[i])为排序后开前i个箱子的总花费,(frac{1}{2}^{n - i})为剩下箱子全为黑/白球的概率(这样的话就不用继续开了 )。

    可会有这样的疑问:上面的概率和不为1,不满足期望的定义呀?不要忘记一开始根据hint就可能知道n个球全为黑色或者全为白色了!这样的花费是0,没有体现在上面的公式中!他们的概率为(2 imes frac{1}{2}^n = frac{1}{2}^{n - 1}),加起来概率和就为1了。

    #include <bits/stdc++.h>
    using namespace std;
    int n;
    double C, w[100005], ans = 0, sum[100005];
    int main() {
    	cin >> n >> C;
    	double tot = 0;
    	for(int i = 1; i <= n; i++) {
    		cin >> w[i];
    		tot += w[i];
    	}
    	sort(w + 1, w + n + 1);
    	for(int i = 1; i <= n; i++) {
    		sum[i] = sum[i - 1] + w[i];
    	}
    	ans += C;
    	for(int i = 1; i <= n - 1; i++) {
    		ans += sum[i] * pow(0.5, n - i);
    	}
    	ans = min(ans, tot);
    	cout << fixed << setprecision(7) << ans;
    	return 0;
    }
    
  • 相关阅读:
    PHP操作Mysql
    python基础篇-爬虫urlparse使用及简单示例
    Mysql 返回JSON值属性的函数 (五)
    Mysql Json函数之更新 (四)
    Mysql Json函数之搜索 (三)
    Mysql Json函数创建 (二)
    Mysql Json函数总览 (一)
    Java8-Consumer、Supplier、Predicate和Function方法总结
    RPC原理及RPC实例分析(转)
    软件版本GA、Beta、RC含义
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/15086493.html
Copyright © 2011-2022 走看看