2021牛客暑期多校训练营5 B - Boxes 期望(线性)

Boxes

在这里插入图片描述

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+w11/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(n1) 时,最后一个箱子没有开启的必要,因此线性递推即可,最后累加.

f ( i ) = f ( i − 1 ) / 2 + w i f(i)=f(i-1)/2+w_i f(i)=f(i1)/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(2i=0n1f(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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值