题目
展开
题目描述
Jeff has become friends with Furik. Now these two are going to play one quite amusing game.
At the beginning of the game Jeff takes a piece of paper and writes down a permutation consisting of nn numbers: p_{1}p
1
, p_{2}p
2
, … , p_{n}p
n
. Then the guys take turns to make moves, Jeff moves first. During his move, Jeff chooses two adjacent permutation elements and then the boy swaps them. During his move, Furic tosses a coin and if the coin shows “heads” he chooses a random pair of adjacent elements with indexes ii and i+1i+1 , for which an inequality p_{i}>p_{i+1} holds, and swaps them. But if the coin shows “tails”, Furik chooses a random pair of adjacent elements with indexes ii and i+1i+1 , for which the inequality p_{i}<p_{i+1} holds, and swaps them. If the coin shows “heads” or “tails” and Furik has multiple ways of adjacent pairs to take, then he uniformly takes one of the pairs. If Furik doesn’t have any pair to take, he tosses a coin one more time. The game ends when the permutation is sorted in the increasing order.
Jeff wants the game to finish as quickly as possible (that is, he wants both players to make as few moves as possible). Help Jeff find the minimum mathematical expectation of the number of moves in the game if he moves optimally well.
You can consider that the coin shows the heads (or tails) with the probability of 5050 percent.
输入格式
The first line contains integer nn (1<=n<=3000)(1<=n<=3000) . The next line contains nn distinct integers p_{1}p
1
, p_{2}p
2
, … , p_{n}p
n
(1<=p_{i}<=n)(1<=p
i
<=n) — the permutation pp . The numbers are separated by spaces.
输出格式
In a single line print a single real value — the answer to the problem. The answer will be considered correct if the absolute or relative error doesn’t exceed 10^{-6}10
−6
.
题意翻译
有一个长度为n的排列p,Jeff每次操作选两个相邻元素交换,Furik每次操作随机执行下列操作之一:
1.对于所有p[i]<p[i+1],随机取一对p[i]与p[i+1]交换
2.对于所有p[i]>p[i+1],随机取一对p[i]与p[i+1]交换
两个操作被选中的概率均为0.5。如果一个操作无法进行,那么furik必定会进行可以进行的操作。
两人轮流操作,Jeff先手。当排列为升序时,游戏结束。 Jeff希望游戏尽早结束,那么在Jeff每次操作最优的情况下,游戏结束时两人操作次数的期望值是多少?。
输入输出样例
输入 #1复制
2
1 2
输出 #1复制
0.000000
输入 #2复制
5
3 5 2 4 1
输出 #2复制
13.000000
说明/提示
In the first test the sequence is already sorted, so the answer is 00 .
思路
显然Jeff的做法选择一个逆序对交换,才会使操作次数最少
先看一个错误思路:
这道题我刚开始的想法时,
f
[
i
]
[
0
/
1
]
f[i][0/1]
f[i][0/1] 表示到达剩余
i
i
i对逆序对的局面当前轮到Jeff/Furik操作的期望步数,因为Fruik的操作是随机的所以,
f
[
i
]
f[i]
f[i]可以转移到,
f
[
i
+
1
]
,
f
[
i
−
1
]
f[i +1], f[i - 1]
f[i+1],f[i−1], Jeff又可以将
f
[
i
+
1
]
f[i + 1]
f[i+1]转移到
f
[
i
]
f[i]
f[i], 显然这样的转移是存在环的(即有后效性)
正解:
考虑将Jeff和Fruik当成一整轮一起操作,那么一轮操作中又
0.5
0.5
0.5的概率减少两个逆序对,
0.5
0.5
0.5的概率使逆序对个数不变。
状态表示:
f
[
i
]
f[i]
f[i] 表示剩余
i
i
i对逆序对的期望步数
初始条件:设求出的逆序对的个数为
n
u
m
num
num,
f
[
n
u
m
]
=
0
f[num] = 0
f[num]=0
状态转移 :
f
[
i
]
=
0.5
∗
(
f
[
i
]
+
2
)
+
0.5
∗
(
f
[
i
+
2
]
+
2
)
f[i] = 0.5 * (f[i] + 2) + 0.5*(f[i + 2] + 2)
f[i]=0.5∗(f[i]+2)+0.5∗(f[i+2]+2)
到这里,我们会发现
f
[
i
]
f[i]
f[i]的转移还是有自环的,怎么办呢?
解方程!我是第一次接触这种操作啊
f
[
i
]
=
f
[
i
]
+
f
[
i
+
2
]
2
+
2
f[i] = \frac{f[i] + f[i + 2]}{2} + 2
f[i]=2f[i]+f[i+2]+2
2
∗
f
[
i
]
=
f
[
i
]
+
f
[
i
+
2
]
+
4
2*f[i] = f[i] + f[i +2] +4
2∗f[i]=f[i]+f[i+2]+4
即
f
[
i
]
=
f
[
i
+
2
]
+
4
f[i] = f[i + 2] + 4
f[i]=f[i+2]+4
但是,当你有奇数个逆序对的时候,游戏不能在整轮操作中完成,还需要再由Jeff操作1次,答案要 +1
因为求逆序对,时间复杂度:
O
(
n
l
o
g
n
)
O(nlogn)
O(nlogn)
代码
#include<bits/stdc++.h>
const int N=3077;
int n,a[N],bit[N],cnt;
void add(int x,int val)
{
for(; x<=n; x+=x&-x) bit[x]+=val;
}
int query(int x)
{
int res=0;
for(; x; x-=x&-x) res+=bit[x];
return res;
}
int main()
{
scanf("%d",&n);
for(int i=1; i<=n; ++i) scanf("%d",&a[i]);
for(int i=n; i>=1; --i)
{
cnt+=query(a[i]-1);
add(a[i],1);
}
printf("%d.000000\n",(cnt<<1)-(cnt&1));
}