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

题意:  两个人轮流游戏,先手交换相邻两个数,后手先抛硬币,正面,就随机找到一对相邻左大右小的数换,反面,就随机找到一对相邻右大左小的数换,直到整个数列升序排列,求最小期望步数。

思路:  由于第一个人每次都会减少一对逆序对,而后手会50%减少一对,50%增加一对,我们把两个人凑起来就是:

50%逆序对不变,50%减少2对

令dp[i]表示减少i个逆序对的步数的期望

dp[i]=dp[i]*0.5+dp[i-2]*0.5+2(要减少i个逆序对期望dp[i]步 = 此次两人共2步+减少dp[i-2]逆序对的期望步数+减少dp[i]逆序対的期望步数)

算出来就是dp[i]=dp[i-2]+4,初值:dp[0]=0,dp[1]=1

故dp数组分奇偶后就是两个等差数列,dp[i]=2*i(i为偶数),dp[i]=2*i-1(i为奇数),逆序对直接n^2暴力即可

#include "bits/stdc++.h"
using namespace std;
#define rep(i, s, n) for(int i=s;i<n;i++)
const int N=;
int a[N];
int main() {
int n;
while(cin >> n){
rep(i, , n) cin >> a[i];
int s = ;
rep(i, , n) rep(j, i + , n) if (a[i] > a[j]) s++;
double ans = s & ? s / * + : s * ;
printf("%.6f\n", ans);
}
return ;
}

Codeforces 351B Jeff and Furik 概率 | DP的更多相关文章

  1. Codeforces 351B Jeff and Furik:概率 + 逆序对【结论题 or dp】

    题目链接:http://codeforces.com/problemset/problem/351/B 题意: 给你一个1到n的排列a[i]. Jeff和Furik轮流操作,Jeff先手. Jeff每 ...

  2. Codeforces 351B Jeff and Furik

    http://codeforces.com/problemset/problem/351/B 题意:两个人轮流游戏,先手交换相邻两个数,后手先抛硬币,正面就左大右小换,反面就右大左小换,随机找到一对数 ...

  3. CodeForces 540D--Bad Luck Island(概率DP)

    貌似竟然是我的第一道概率DP.. 手机码代码真不舒服.... /************************************************ Memory: 67248 KB Ti ...

  4. codeforces 148D Bag of mice(概率dp)

    题意:给你w个白色小鼠和b个黑色小鼠,把他们放到袋子里,princess先取,dragon后取,princess取的时候从剩下的当当中任意取一个,dragon取得时候也是从剩下的时候任取一个,但是取完 ...

  5. CodeForces 24D Broken robot (概率DP)

    D. Broken robot time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  6. codeforces 352D - Jeff and Furik【期望dp】

    首先恋人操作过一轮之后逆序对不会变多,所以设f[i]为把i个逆序对消掉的期望次数,f[i]=0.5f[i-2]+0.5f[i]+2,化简然后递推即可 #include<iostream> ...

  7. Codeforces 148D Bag of mice 概率dp(水

    题目链接:http://codeforces.com/problemset/problem/148/D 题意: 原来袋子里有w仅仅白鼠和b仅仅黑鼠 龙和王妃轮流从袋子里抓老鼠. 谁先抓到白色老师谁就赢 ...

  8. CodeForces 148D-Bag of mice(概率dp)

    题意: 袋子里有w个白球b个黑球,现在两个人轮流每次取一个球(不放回),先取到白球的获胜,当后手取走一个球时,袋子里的球会随机的漏掉一个,问先手获胜的概率. 分析: dp[i][j]表示袋子中i个白球 ...

  9. Codeforces 1151F Sonya and Informatics (概率dp)

    大意: 给定01序列, 求随机交换k次后, 序列升序的概率. 假设一共$tot$个$0$, 设交换$i$次后前$tot$个数中有$j$个$0$的方案数为$dp[i][j]$, 答案即为$\frac{d ...

随机推荐

  1. 互评Alpha版本——Thunder团队

    基于NABCD评论作品 Hello World! :http://www.cnblogs.com/120626fj/p/7807544.html 欢迎来怼 :http://www.cnblogs.co ...

  2. BluetoothClass详解

    一. BluetoothClass简介 1. 继承关系 public final class BluetoothClass extends Object implements Parcelable 该 ...

  3. String 和 CharSequence 关系与区别

    String 继承于CharSequence,也就是说String也是CharSequence类型. CharSequence是一个接口,它只包括length(), charAt(int index) ...

  4. RXSwift--登录注册那点事

    在iOS学习中登录注册是一个万能的可以拿出来实战的demo.接下来我们就从登录开始入手,PS:如果你对RXSwift中的概念和一些常用的函数不清楚可以参考这篇文章(可能打开比较慢请耐心等待).开始直接 ...

  5. TCP系列34—窗口管理&流控—8、缓存自动调整

    一.概述 我们之前介绍过一种具有大的带宽时延乘积(band-delay product.BDP)的网络,这种网络称为长肥网络(LongFatNetwork,即LFN).我们想象一种简单的场景,假设发送 ...

  6. <Android>tab选项卡

    1.继承TabActivity实现 a)         在布局文件中使用FrameLayout列出Tab组件及Tab中的内容组件 b)        Activity要继承TabActivity c ...

  7. Ligerui首页的快速搭建

    一.目录 1.多层架构+MVC+EF+AUTOFAC+AUTOMAPPER: 2.MVC中验证码的实现(经常用,记录备用) 3.Ligerui首页的快速搭建 4.Ligerui Grid组件--学生信 ...

  8. jsp连接MYSQL数据库教程(文字+图)

    步骤: 1.在mysql官网下载JDBC驱动程序.网址:https://dev.mysql.com/downloads/connector/j/ 2.把里面的jar包(mysql-connector- ...

  9. python实现post请求

    今天无论如何都要留下一些什么东西... 可以说今天学到一个新的一个东西,也需要分享出来,给更多的人去使用. 今天爬取的数据里面是客户端向服务器端发送加密过的token和一些页码之类的一个数据.(我主要 ...

  10. [OS] 操作系统-进程线程-经典面试笔试题

    题目转自:http://blog.csdn.net/morewindows/article/details/7392749 ·线程的基本概念.线程的基本状态及状态之间的关系? 线程,有时称为轻量级进程 ...