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由小到大的顺序开箱(因为每个箱子概率完全一样且相互独立,如果先开小的开到某个箱子可以根据推出来的黑球的数量直接判断后面箱子里球的颜色就不用继续开了。这样算出来的答案是,其中为排序后开前i个箱子的总花费,为剩下箱子全为黑/白球的概率(这样的话就不用继续开了 )。
可会有这样的疑问:上面的概率和不为1,不满足期望的定义呀?不要忘记一开始根据hint就可能知道n个球全为黑色或者全为白色了!这样的花费是0,没有体现在上面的公式中!他们的概率为,加起来概率和就为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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2020-08-01 深度学习笔记二:卷积神经网络(CNN)