链接:https://www.nowcoder.com/acm/contest/143/F
来源:牛客网
题目描述
Kanade has n boxes , the i-th box has p[i] probability to have an diamond of d[i] size.
At the beginning , Kanade has a diamond of 0 size. She will open the boxes from 1-st to n-th. When she open a box,if there is a diamond in it and it's bigger than the diamond of her , she will replace it with her diamond.
Now you need to calculate the expect number of replacements.
You only need to output the answer module 998244353.
Notice: If x%998244353=y*d %998244353 ,then we denote that x/y%998244353 =d%998244353
输入描述:
The first line has one integer n. Then there are n lines. each line has two integers p[i]*100 and d[i].
输出描述:
Output the answer module 998244353
示例1
输入
复制
3 50 1 50 2 50 3
输出
复制
499122178
备注:
1<= n <= 100000 1<=p[i]*100 <=100 1<=d[i]<=10^9
题意:有n个盒子,每个盒子中有p[i]的概率有d[i]大小的钻石,初始有大小为0的钻石,每遇到一个比自己当前大的钻石就要进行交换,求交换的期望。
题解:对于每个钻石进行讨论,这个钻石进行交换的可能性是前面所有比这个大的钻石都不产生且这个钻石产生的概率,那么根据d从大到小进行排序,然后求出每个钻石交换的概率求和即可,还要注意概率是p[i]*100,那么直接乘100的逆元即可,可以用树状数组维护前缀积,维护当前钻石前方比自己大的钻石的不产生的概率的乘积,因为维护乘积所以树状数组要初始化为1.
#include<bits/stdc++.h>
using namespace std;
const int inf=1e9;
const int maxn=1e5+5;
const int mod =998244353;
typedef long long ll;
int n;
struct Node
{
ll p,d;
int id;
bool operator < (const Node & rhs) const
{
return d>rhs.d || d==rhs.d && id<rhs.id;
}
}a[maxn];
ll tree[maxn];
ll qpow(ll a,int b=mod-2)
{
ll ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return ans;
}
void add(int x,ll v)
{
for(int i=x;i<maxn;i+=i&-i)
tree[i]=(tree[i]*v)%mod;
}
ll ask(int x)
{
ll ans=1;
for(int i=x;i;i-=i&-i)
ans=(ans*tree[i])%mod;
return ans;
}
int main()
{
for(int i=1;i<=maxn;i++) tree[i]=1;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%lld%lld",&a[i].p,&a[i].d);
a[i].id=i+1;
}
sort(a,a+n);
ll ans=0;
ll inv=qpow(100);
for(int i=0;i<n;i++)
{
Node t=a[i];
ll tmp=ask(t.id-1)*(inv*t.p%mod)%mod;
ans=(ans+tmp)%mod;
add(t.id,inv*(100-t.p)%mod);
}
printf("%lld\n",ans);
return 0;
}