题目意思就是从前往后有n个箱子,每个箱子中有p[i]的概率会出现价值为d[i]的钻石,当箱子内的钻石价值大于当前手上的钻石的价值的时候,将手上钻石跟箱子内的钻石交换,问最后交换次数的期望。
思路:结果要求的是交换次数的期望,我们可以将每个箱子交换的期望求出来相加即可。那么显而易见的是,当前箱子交换的概率即为该箱子之前的所有d[i]大于当前箱子的箱子内的钻石都不出现的概率乘以当前箱子出现钻石的概率。
我们可以首先将d从大到小排序,用线段树维护1 - pi的前缀积。每次插入前查询在它前面比他大的概率的乘积(因为从大到小插入了,所以线段树里面的插入的都是d比他大的)由于维护的是乘积,所以我们线段树初值要初始化成1。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long int
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
const int mod = 998244353;
const LL inv = 828542813;
int n;
LL tre[100100 << 2];
struct node
{
int p, x, id;
}s[100100];
bool cmp(node a, node b)
{
return a.x==b.x?a.id<b.id:a.x > b.x;
}
void build(int rt, int l, int r)
{
tre[rt] = 1;
if (l == r) return;
int m = l + r >> 1;
build(lson);
build(rson);
}
void update(int rt, int l, int r, int pos, int x)
{
if (l == r)
{
tre[rt] = (100 - x)*inv%mod;
return;
}
int m = l + r >> 1;
if (pos <= m) update(lson, pos, x);
if (pos > m) update(rson, pos, x);
tre[rt] = tre[rt << 1] * tre[rt << 1 | 1] % mod;
}
LL query(int rt, int l, int r, int L, int R)
{
if (L <= l&&r <= R)
return tre[rt];
int m = l + r >> 1;
LL res = 1;
if (L <= m) (res *= query(lson, L, R)) %= mod;
if (R > m) (res *= query(rson, L, R)) %= mod;
return res;
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d%d", &s[i].p, &s[i].x);
s[i].id = i + 1;
}
sort(s, s + n, cmp);
build(1, 1, n);
LL res = 0;
for (int i = 0; i < n; i++)
{
res = (res + query(1, 1, n, 1, s[i].id)*s[i].p%mod*inv) % mod;
update(1, 1, n, s[i].id, s[i].p);
}
printf("%lld\n", res);
return 0;
}