CF&&CC百套计划3 Codeforces Round #204 (Div. 1) B. Jeff and Furik

http://codeforces.com/contest/351/problem/B

 

题意:

给出一个n的排列

第一个人任选两个相邻数交换位置

第二个人有一半的概率交换相邻的第一个数>第二个数的位置;有一半的概率交换相邻第一个数<第二个数的位置

然后两人轮换

问使序列升序,期望最少操作次数

 

序列升序即逆序对数为0

dp[i]表示 当前逆序对还剩i对时,先手期望最少操作次数

先手在最优解的情况下一定交换 第一个数>第二个数的位置,即减少1个逆序对

后手等概率增加或减少1个逆序对

dp[i]=1+1+dp[i-1-1]*0.5+dp[i-1+1]*0.5

即dp[i]=4+dp[i-2]

所以,若逆序对数为m

ans=m*2-(m&1)

 

#include<cstdio>

using namespace std;

#define N 3001

int a[N];

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;++i) scanf("%d",&a[i]);
    int m=0;
    for(int i=1;i<=n;++i)
         for(int j=1;j<i;++j)
             if(a[j]>a[i]) m++;
    printf("%d.000000",m*2-(m&1));
}

 

B. Jeff and Furik
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 n numbers: p1, p2, ..., pn. 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 i and i + 1, for which an inequality pi > pi + 1 holds, and swaps them. But if the coin shows "tails", Furik chooses a random pair of adjacent elements with indexes i and i + 1, for which the inequality pi < pi + 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 50 percent.

Input

The first line contains integer n (1 ≤ n ≤ 3000). The next line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the permutation p. The numbers are separated by spaces.

Output

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.

Examples
input
2
1 2
output
0.000000
input
5
3 5 2 4 1
output
13.000000
Note

In the first test the sequence is already sorted, so the answer is 0.

 

 

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/8067922.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值