solution
期望题,两种情况:
1. 1. 1. 花费 C C C ,得知n个箱子中的黑白球个数.
2. 2. 2. 不花费 C C C,将所有箱子打开.
第二种情况很简单,因此我们展开考虑第一种情况:
假设 f ( x ) f(x) f(x) 表示开启 x x x 个箱子时完成要求,所需的费用.
f ( 0 ) = 0. f(0)=0. f(0)=0.
先将箱子进行排序,优先开启费用小的箱子,对于黑球,可能有 0 , 1 , 2 , . . , n 0,1,2,..,n 0,1,2,..,n 个,因此我们开启第一个箱子的时候,花费 w 1 w_1 w1,如果此时游戏结束 f ( 1 ) = f ( 0 ) / 2 + w 1 , 1 / 2 f(1)=f(0)/2+w_1,1/2 f(1)=f(0)/2+w1,1/2的概率是我们想要的,否则继续, f ( 2 ) = f ( 1 ) / 2 + w 2 f(2)=f(1)/2+w_2 f(2)=f(1)/2+w2,当你进行到 f ( n − 1 ) f(n-1) f(n−1) 时,最后一个箱子没有开启的必要,因此线性递推即可,最后累加.
f ( i ) = f ( i − 1 ) / 2 + w i f(i)=f(i-1)/2+w_i f(i)=f(i−1)/2+wi
A n s w e r = m i n ( ∑ i = 0 n − 1 f ( i ) 2 + C , ∑ w i ) Answer=min(\frac{\sum_{i=0}^{n-1}f(i)}{2}+C,\sum w_i) Answer=min(2∑i=0n−1f(i)+C,∑wi)
code
/*SiberianSquirrel*//*CuteKiloFish*/
#include <bits/stdc++.h>
using namespace std;
#define gcd(a,b) __gcd(a,b)
#define Inv(x) quick_pow(x, mod - 2)
#define Polynomial vector<int>
#define DEBUG(x, y) cout << x << ": " << y << '\n';
#define mem(a, x) memset(a, x, sizeof a)
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using i16 = short;
const ld pi = acos(-1.0);
const ll mod = 998244353, mod_g = 3, img = 86583718;
//const ll mod = 1e9 + 7;
//-----------------------------------------------------------------IO Template
namespace StandardIO {
template<typename T> inline void read(T &x) {
x = 0; T f = 1;
char c = getchar();
for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = -1;
for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
x *= f;
}
template<typename T> inline void write(T x) {
if (x < 0) putchar('-'), x *= -1;
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
}
using namespace StandardIO;
// -------------------------------------------------------------------------------BASIC_MATH
namespace BASIC_MATH {
ll quick_pow(ll ans, ll p, ll res = 1) {
for(; p; p >>= 1, ans = ans * ans % mod)
if(p & 1) res = res * ans % mod;
return res % mod;
}
}
using namespace BASIC_MATH;
inline void solve() {
int n; double p; cin >> n >> p; vector<double> W(n + 1);
double res = 0, ans;
for(int i = 1; i <= n; ++ i) { cin >> W[i]; res += W[i]; }
sort(W.begin(), W.begin() + 1 + n);
double temp = res;
res = ans = 0;
for(int i = 1; i < n; ++ i) {
res = res / 2.0 + W[i];
ans += res;
}
cout << fixed << setprecision(10) << min(temp, ans / 2.0 + p) << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifdef ACM_LOCAL
freopen("input", "r", stdin);
freopen("output", "w", stdout);
signed test_index_for_debug = 1;
char acm_local_for_debug = 0;
do {
if (acm_local_for_debug == '$') exit(0);
if (test_index_for_debug > 20)
throw runtime_error("Check the stdin!!!");
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful!" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
} while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
solve();
#endif
return 0;
}