take
take
題解參考
題目描述
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
輸入
復制
輸出
復制
備注:
1<= n <= 100000
1<=p[i]*100 <=100
1<=d[i]<=10^9
題意:
有n個箱子,第i個盒子具有p [i]大小為d [i]的鉆石的概率。
從1號開始依次打開,如果比手中的寶石大就替換它,計算期望的更換次數
(一開始默認手中鉆石為0)
題解:
一旦扯到概率我就感覺好復雜。。。
首先我們要知道期望有可加性的,也就是E(X+Y)=E(X)+E(Y)
所以,所有交換次數的期望就等于每個寶箱打開后交換次數的期望和,而當個寶箱交換次數無非是0或1(即交換與不交換),所以如果一個寶箱能夠產生交換次數的話,就會對答案產生貢獻了
所以我們就要去看每一個寶箱到底能否做出貢獻,這怎么看?交換的情況是當前的寶石更大,但是在此寶箱之前可以有更大的寶石,那我們就要使該寶箱打開后不出現寶石就可以了
總結下,對于每個寶箱,我們使在其之前擁有比當前寶箱更大的鉆石的寶箱打開后不出現鉆石
式子如圖
如果來
代碼:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int mod=998244353; const int maxn=1e5+10; int c[maxn]; struct node {int d,id;ll p;bool operator<(const node &a)const{if(d==a.d) return id<a.id;return d>a.d;} }q[maxn];int lowbit(int x) {return x&(-x); } void add(int p,ll v) {while(p<=maxn){c[p]=(c[p]*v)%mod;p+=lowbit(p);} }ll sum(int p) {ll res=1;while(p){res=(res*c[p])%mod;p-=lowbit(p);}return res; }ll quickpow(ll a,ll b,ll m) {a=a%m;ll ans=1;while(b){if(b&1) ans=(ans*a)%mod;b>>=1;a=(a*a)%mod;}return ans; } int main() {int n;scanf("%d",&n);ll inv=quickpow(100,mod-2,mod);for(int i=1;i<=maxn;i++) c[i]=1;for(int i=1;i<=n;i++){scanf("%lld %d",&q[i].p,&q[i].d);q[i].p=(q[i].p*inv)%mod;q[i].id=i;}sort(q+1,q+n+1);ll ans=0;for(int i=1;i<=n;i++){ans=(ans+(1LL*sum(q[i].id)*q[i].p%mod))%mod;add(q[i].id,(1-q[i].p+mod)%mod);}printf("%lld\n",ans); }總結
- 上一篇: 天气情况图像分类练习赛 第三阶段(赛中感
- 下一篇: 数字串