牛客多校 take (期望 )

n个箱子,每个里面有可能有大小为x的钻石,如果打开后里面的钻石大于当前手里的,那么就换掉.
问你置换次数的期望.
转换成每个箱子打开后置换的概率之和,树状数组维护即可.

#include 
#include 
#include 
#include 
#define debug(x) std::cerr << #x << " = " << (x) << std::endl
using namespace std;
typedef long long LL;
const int MAXN = 2e5 + 17;
const int MOD = 998244353;
LL qm(LL a,LL b)
{
    LL ret = 1;
    while(b)
    {
        if(b&1) ret = a*ret%MOD;
        a =a*a%MOD;
        b>>=1;
    }
    return ret;
}
LL bit[MAXN],n;
void add(LL p,LL x)
{
    while(p<=n)
    {
        bit[p] = (bit[p]*x)%MOD;
        p += p&-p;
    }
}
LL sum(LL p)
{
    LL ret = 1;
    while(p>0)
    {
        ret = (ret*bit[p])%MOD;
        p -= p&-p;
    }
    return ret;
}
int main()
{
#ifdef noob
    freopen("Input.txt", "r", stdin);
    freopen("Output.txt", "w", stdout);
#endif
    cin>>n;
    vectorint,int,int > > all;
    vector<int > op;
    for (int i = 0; i < n; ++i)
    {
        bit[i+1] = 1;
        LL p,d;
        cin>>p>>d;
        op.push_back((100-p)*qm(100,MOD-2)%MOD);
        p = (p*qm(100,MOD-2))%MOD;
        all.push_back(make_tuple(d,p,i+1));
    }
    sort(all.begin(), all.end());
    LL up = 0;
    for (int i = n-1; i > -1; --i)
    {
        LL d,p,id;
        tie(d,p,id) = all[i];
        up = (up+(p*sum(id))%MOD)%MOD;
        add(id,op[id-1]);
    }
    cout<return 0;
}

想了很久,不知道怎么计算这个期望.
后来看了题解,他把这个期望,转换成每个箱子打开后置换的概率之和.
为什么呢?我也不知道为什么.
E(x+y) = E(x) + E(Y)
以后期望都这样搞一下.

你可能感兴趣的:(牛客多校 take (期望 ))