Codeforces 351B Jeff and Furik
http://codeforces.com/problemset/problem/351/B
题意:两个人轮流游戏,先手交换相邻两个数,后手先抛硬币,正面就左大右小换,反面就右大左小换,随机找到一对数,直到整个数列上升位置,求最小期望步数。
思路:由于第一个人每次都会减少一对逆序对,而后手会50%减少一对,50%增加一对,我们把两个人凑起来就是:
50%逆序对不变,50%减少2对
f[i]=f[i]*0.5+f[i-2]*0.5+2
算出来就是f[i]=f[i-2]+4,初值:f[0]=0,f[1]=1
1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 #include<algorithm> 5 #include<iostream> 6 int n,a[3005],c[3005]; 7 int f[3005*3005]; 8 int read(){ 9 int t=0,f=1;char ch=getchar(); 10 while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} 11 while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();} 12 return t*f; 13 } 14 int lowbit(int x){ 15 return x&(-x); 16 } 17 void add(int x){ 18 for (int i=x;i;i-=lowbit(i)){ 19 c[i]++; 20 } 21 } 22 int ask(int x){ 23 int res=0; 24 for (int i=x;i<=n;i+=lowbit(i)){ 25 res+=c[i]; 26 } 27 return res; 28 } 29 int main(){ 30 n=read();int ans=0; 31 for (int i=1;i<=n;i++) a[i]=read(); 32 for (int i=1;i<=n;i++) ans+=ask(a[i]),add(a[i]); 33 f[0]=0;f[1]=1; 34 for (int i=2;i<=ans;i++) f[i]=f[i-2]+4; 35 printf("%.6f\n",(double)f[ans]); 36 }