D. 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.

 题意:两个人玩游戏,给n个数,第一个人选取相邻两个递减的数交换顺序,第二个人一半的概率选取相邻两个递减的数交换顺序,一半的概率选取相邻两个递增的数交换顺序。两个人轮流操作,求整个数列变成递增数列所需交换次数的期望。

思路:我们知道,一个序列中, a[i] > a[j]这样的数对称为逆序数对,而题目的意思其实就是求把数列中的逆序对的数量变成0时所要的最小步数,于是可以这么做,求出逆序对的数量,然后算出递推公式,找规律,算到最后发现是两个等差数列。。。以下是递推过程:

假设d[i]为当逆序对为i对时所需要的步数,那么d[0]=0,d[1]=1,这是已知的,当i>=2,d[i]=0.5*d[i-1-1]+0.5*d[i-1+1]+1+1,化简得d[i]=d[i-2]+4;

所以,d[0]=0,d[2]=4,d[4]=8,d[6]=12;

   d[1]=1,d[3]=5,d[5]=9,d[7]=13;

所以,当i为偶数时,d[i]=i*2;

       当i为奇数时,d[i]=i/2*4+1;

#include<bits/stdc++.h>
using namespace std;
int main() {
int n, cnt = ;
double a[];
cin >> n;
for(int i = ; i < n; i++)
cin >> a[i];
for(int i = ; i < n; i++) {
for(int j = i + ; j < n; j++) {
if(a[i] > a[j])
cnt++;//逆序对个数
}
}
if(cnt % == )
printf("%f\n", (cnt * ));
else
printf("%f\n", (cnt / * + ));
return ; }

  

Codeforces Round #204 (Div. 2)->D. Jeff and Furik的更多相关文章

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

    http://codeforces.com/contest/351/problem/B 题意: 给出一个n的排列 第一个人任选两个相邻数交换位置 第二个人有一半的概率交换相邻的第一个数>第二个数 ...

  2. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding

    http://codeforces.com/problemset/problem/351/A 题意: 2*n个数,选n个数上取整,n个数下取整 最小化 abs(取整之后数的和-原来数的和) 先使所有的 ...

  3. Codeforces Round #204 (Div. 2)->C. Jeff and Rounding

    C. Jeff and Rounding time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) E. Jeff and Permutation

    http://codeforces.com/contest/351/problem/E 题意: 给出一些数,可以改变任意数的正负,使序列的逆序对数量最少 因为可以任意加负号,所以可以先把所有数看作正数 ...

  5. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods

    http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...

  6. Codeforces Round #204 (Div. 2)->B. Jeff and Periods

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

  7. Codeforces Round #204 (Div. 2) A.Jeff and Digits

    因为数字只含有5或0,如果要被90整除的话必须含有0,否则输出-1 如果含有0的话,就只需考虑组合的数字之和是9的倍数,只需要看最大的5的个数能否被9整数 #include <iostream& ...

  8. Codeforces Round #204 (Div. 2) C. Jeff and Rounding——数学规律

    给予N*2个数字,改变其中的N个向上进位,N个向下进位,使最后得到得数与原来数的差的绝对值最小 考虑小数点后面的数字,如果这些数都非零,则就是  abs(原数小数部分相加-1*n), 多一个0 则 m ...

  9. Codeforces Round #204 (Div. 2)

    D. Jeff and Furik time limit per test 1 second memory limit per test 256 megabytes input standard in ...

随机推荐

  1. linux下的循环命令写法

    直切正题 方法一:利用while do循环,举例,while true;do ls;sleep 1;done  解释,该命令为每秒执行ls查询命令,sleep 1 为每秒循环,其他命令可直接替换 ls ...

  2. ORACLE DG之参数详解

    1.DB_NAME 数据库名字,需要保持同一个Data Guard中所有数据库DB_NAME相同. 主库和备库相同 DB_NAME='chicago' DB_NAME='chicago' 2.DB_U ...

  3. 内存详解--理解 JVM 如何使用 AIX 上的本机内存

    转自---http://www.ibm.com/developerworks/cn/java/j-nativememory-aix/

  4. linux 使用 pyodbc 访问 ms sqlserver 数据库

    一.安装linux下Sqlserver的驱动程序: 我们先来安装SqlServer的驱动程序,再安装ODBC相关的,目的是想尽快可以进行测试,因为在安装完Freetds之后,我们马上就可以在linux ...

  5. python爬取网站数据

    开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...

  6. C# 页面抓取类

    抓取网站页面的内容,简单的类应用,代码如下: /// <summary> /// 获取页面内容 /// </summary> /// <param name=" ...

  7. lnmp的使用

    命令 1.状态管理 lnmp {start|stop|reload|restart|kill|status} 2.添加虚拟host lnmp vhost add

  8. Tutorial: Importing and analyzing data from a Web Page using Power BI Desktop

    In this tutorial, you will learn how to import a table of data from a Web page and create a report t ...

  9. jquery方法回到顶部代码

    <style> /*默认样式,主要是position:fixed实现屏幕绝对定位*/ #gotoTop{display:none;position:fixed;top:75%;left:5 ...

  10. 引用类型a=b

    List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); Cache["Key ...