牛客网暑期ACM多校训练营(第五场)F-take

10 篇文章 0 订阅
2 篇文章 0 订阅

题目意思就是从前往后有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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值