题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4481
太弱了这种题都要看半天TJ...:https://blog.csdn.net/chai_jing/article/details/72870473
等比数列求和的公式是 ( a1 * ( 1 - q^n ) ) / ( 1 - q ),如果 n 是无穷大的话,若 0 < q < 1,那么 q^n 趋近于 0 ,所以公式就成了 a1 / ( 1 - q );
计算每个女性选中每个男性的概率,放进树状数组里求逆序对期望即可。
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long double ld; int const maxn=5e5+5; int n,m; ld f[maxn],ans,p; struct N{int a,b;}q[maxn]; bool cmp(N x,N y){return (x.a==y.a)?x.b<y.b:x.a<y.a;} void add(int x,ld v){for(;x<=n;x+=(x&-x))f[x]+=v;} ld query(int x){ld ret=0; for(;x;x-=(x&-x))ret+=f[x]; return ret;} int main() { scanf("%d%d%Lf",&n,&m,&p); for(int i=1;i<=m;i++)scanf("%d%d",&q[i].a,&q[i].b); sort(q+1,q+m+1,cmp); for(int i=1,lst;i<=m;i=lst+1) { lst=i; ld up=p,dn=1; while(q[lst+1].a==q[lst].a)lst++; for(int j=1;j<=lst-i+1;j++)dn*=(1-p); dn=1-dn; for(int j=i;j<=lst;j++) { if(j!=i)up*=(1-p); add(q[j].b,up/dn); ans+=(up/dn)*(query(n)-query(q[j].b)); } } printf("%.2Lf ",ans); return 0; }