Topic Links:
Codeforces 351B
Main topic:
Give a game, the initiator select a pair of adjacent number Exchange location, the back of a random logarithm, and then toss a coin to decide whether to exchange, if not exchange, then re-find. Ask the approximate sequence of the desired number of cloth to take the optimal strategy.
Topic Analysis:
- Definition Dp[i] represents the expectation of reducing the number of steps in the I reverse order.
- d P[I]=1+1+d P[I?2]?0.5+d P[I?1+1]]?0.5
- Because the initiator can choose, so it will always take action to reduce an inverse pair.
- We get dp[i] = 4 + dp[i-2],dp[0] = 0, dp[1] = 1;
AC Code:
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#define MAX 3007using namespace STD;intN,a[max];intMain () { while( ~scanf("%d", &n)) { for(inti =0; I < n; i++)scanf("%d", &a[i]);intm =0; for(inti =1; I < n; i++) for(intj =0; J < I; J + +)if(A[j] > A[i]) m++;intAns =0;if(m&1) ans =1+ m/2*4;ElseAns = m*2;printf("%.7f\n", (Double) ans); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces 351B B. Jeff and Furik (probability)