链接:https://ac.nowcoder.com/acm/contest/11256/B
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld
题目描述
There’re nn_{}n 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 12\frac{1}{2}21, 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_{}1 to nn_{}n, the cost to open the box ii_{}i is wiw_iwi, 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_{}C 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\le n \le 10^5)n (1≤n≤105) and a decimal C (0<C≤109)C~(0 < C \le 10^9)C (0<C≤109), representing the number of boxes and the cost to get a hint from Gromah.
The second line contains nn_{}n decimals w1,w2,⋯ ,wn (0<wi≤109)w_1, w_2, \cdots, w_n~(0 < w_i \le 10^9)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}10−6.
示例1
输入
[复制](javascript:void(0)😉
2 0.1
1 1
输出
[复制](javascript:void(0)😉
0.6
说明
For the first test case, you can pay 0.10.1_{}0.1 cost to get a hint from Gromah. If the number of black balls is 00_{}0 or 22_{}2, you will know the colors in each box. This case has a probability of 12\frac{1}{2}21. 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 + \frac{1}{2} \times 1 = 0.60.1+21×1=0.6.
示例2
输入
[复制](javascript:void(0)😉
4 0.123456
1 1 1 1
输出
[复制](javascript:void(0)😉
2.248456
解释与代码
题意:
给你n个装有白球或者黑球的盒子,其中盒子里是白球的概率为1/2
打开第i个盒子的代价是w[i],在你没有打开盒子之前你无法知道盒子中的球的颜色
而你最多有一次机会可以使用代价C来知道剩余的盒子里面还有多少黑球
问你最小成本的数学期望是多少?
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ppb pop_back
#define lbnd lower_bound
#define ubnd upper_bound
#define endl '\n'
#define mll map<ll,ll>
#define msl map<string,ll>
#define mls map<ll, string>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,a,b) for(ll i=b-1;i>=a;i--)
#define trav(a, x) for(auto& a : x)
#define pll pair<ll,ll>
#define vl vector<ll>
#define vll vector<pair<ll, ll>>
#define vs vector<string>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (ll)x.size()
#define hell 1000000007
#define DEBUG cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x) cerr << #x << " is " << x << endl;
#define ini(a) memset(a,0,sizeof(a))
#define ini2(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define sc ll T;cin>>T;for(ll Q=1;Q<=T;Q++)
#define lowbit(x) x&(-x)
#define pr printf
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \
(void)(cout << "L" << __LINE__ \
<< ": " << #x << " = " << (x) << '\n')
#define TIE \
cin.tie(0);cout.tie(0);\
ios::sync_with_stdio(false);
//#define long long int
//using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+10;
const ll N = 1000000007;
const ull N2 = 1000000007;
typedef struct LNode{
int coef,index;
struct LNode *next;
}LNode,*LinkList;
bool cmp(int a,int b){
return a>b;
}
double cb[maxn];
void solve(){
int n;
double C, ans = 0, f = 1.0, sum = 0;
scanf("%d %lf", &n, &C);
for (int i=1; i<=n; i++) {
scanf("%lf",&cb[i]);
sum += cb[i];
}
sort(cb+1, cb+1+n);
for (int i=n; i; i--) {
ans+=(1-f)*cb[i];
f/=2;
}
printf("%.8lf\n",min(ans+C, sum));
}
int main()
{
// TIE;
// #ifndef ONLINE_JUDGE
// freopen ("input.txt","r",stdin);
// #else
// #endif
solve();
// sc{solve();}
// sc{cout<<"Case "<<Q<<":"<<endl;solve();}
}