题意:有 n 个盒子,里面有一个白球或黑球,是白球的概率是1/2,开每个盒子的代价是wi,可以选择用 c 的代价问 Gromah盒子里的黑球总数。问:可以确定所有盒子里的球的颜色的最小代价是多少并输出,结果要求误差小于10^-6。
两种方案:一种是不问Gromah,开所有的盒子即确定所有盒子里的球的颜色。另一种是最开始就问Gromah何求的总个数,然后将w序列从小到大排列,求最小的代价的期望。从前往后开,只需要开到sum_i为止的条件是,从i到n(之后)的盒子球全是同色的。
概率为 1.0/pow(2,n-i)
期望为 sum[i] * 1.0/pow(2,n-i)
总期望为∑sum[i] * 1.0/pow(2,n-i)然后,比较两个方案,选择代价最小的输出。
- 注意第二种方案的总期望是 i=1 到 n-1,因为已经花 c 的代价知道了黑球总数,最坏的结果也只是开到n-1个盒子就可以确定最后一个盒子的球的颜色。
accode:
#include <bits/stdc++.h>
using namespace std;
#define ios std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
typedef long double ld;
typedef long long ll;
const int maxn = 1e5 + 10;
double w[maxn], sum[maxn];
double ans;
double qpd(double a, ll b)
{
double res = 1;
while(b)
{
if(b & 1)
res *= a;
a *= a;
b>>=1;
}
return res;
}
int main()
{
ios;
memset(w, 0, sizeof(w));
memset(sum, 0, sizeof(sum));
int n;
double c;
scanf("%d %lf", &n, &c);
for(int i = 1; i <= n; i++)
scanf("%lf", &w[i]);
sort(w+1,w+1+n);//从小到大
for(int i = 1; i <= n; i++)
sum[i] = sum[i-1] + w[i];
ans = c;
for(int i = 1; i < n; i++)//已知黑球个数,最多就开n-1个就可以知道全部答案
{
ans += sum[i] * (1.0 / (double)qpd(2, n-i));
}
ans = min(sum[n], ans);
printf("%.7lf\n", ans);
return 0;
}