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=;i<=n;++i) scanf("%d",&a[i]);
int m=;
for(int i=;i<=n;++i)
for(int j=;j<i;++j)
if(a[j]>a[i]) m++;
printf("%d.000000",m*-(m&));
}
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.

CF&&CC百套计划3 Codeforces Round #204 (Div. 1) B. Jeff and Furik的更多相关文章

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

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

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

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

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

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

  4. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits

    http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...

  5. CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence

    http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...

  6. CF&&CC百套计划1 Codeforces Round #449 C. Willem, Chtholly and Seniorious (Old Driver Tree)

    http://codeforces.com/problemset/problem/896/C 题意: 对于一个随机序列,执行以下操作: 区间赋值 区间加 区间求第k小 区间求k次幂的和 对于随机序列, ...

  7. CF&&CC百套计划1 Codeforces Round #449 B. Ithea Plays With Chtholly

    http://codeforces.com/contest/896/problem/B 题意: 交互题 n张卡片填m个1到c之间的数,1<=n*ceil(c/2)<=m 最后填出一个单调非 ...

  8. CF&&CC百套计划1 Codeforces Round #449 A. Nephren gives a riddle

    http://codeforces.com/contest/896/problem/A 第i个字符串嵌套第i-1个字符串 求第n个字符串的第k个字母 dfs #include<map> # ...

  9. Codeforces Round #204 (Div. 2)->D. Jeff and Furik

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

随机推荐

  1. HDU 4336 Card Collector 期望dp+状压

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4336 Card Collector Time Limit: 2000/1000 MS (Java/O ...

  2. “吃神么,买神么”的第一个Sprint计划(第三天)

    “吃神么,买神么”项目Sprint计划 ——5.23  星期六(第三天)立会内容与进度 摘要:今天的立会主要是报告进度以及遇到的困难. 进度:logo正在进行中,其他基本没什么问题.都确定要做出来的大 ...

  3. Results the mutual for the first time(alpha阶段总结)

    由于前天听大家的成果展时,做得笔记不够完善,有一两个组找不到信息,如果没有评到的组望谅解. 分数分配: 由于组内某些原因,我们现重新分组: 试用版: 总结前阶段的工作: 在前一段时间,我们第一个spr ...

  4. Internet History, Technology and Security (Week5.2)

    Week5 Now, I want to make it real clear that, when I give you a 15 minute video of an amazing invent ...

  5. 项目冲刺Beta第二篇博客

    Beta版本冲刺计划安排 1.当天站立式会议照片: 2.工作分工: 团队成员 分工 张洪滨060  排行榜界面美化 陈敬轩059  注册成功界面美化 黄兴067  登录界面美化 林国梽068  答题界 ...

  6. 读书笔记-《Linux/Unix设计思想》

    本书主要讲的是Unix程序设计思想,具体涉及到linux的内容不多. 整本书的一个基本出发点是开源.其中主要强调的观点包括: 1.小即是美 作者持有的主要观点是程序应该以小为美.小程序实现小功能,每个 ...

  7. modify headers插件的使用

    Modity headers是firefox浏览器的一个插件,作用是改变http请求的IP地址 (一)在firefox中添加该插件 步骤一:打开firefox浏览器,打开地址: https://add ...

  8. POJ3041_Asteroids

    这个题目说,有一个N*N的规格的方格.某些格子里有*号,每次可以消除一行或者一列中所有的*号.最少需要消多少次? 新学到的,什么什么定理,最少点覆盖等于最大匹配数. 这个定理可以这样来理解(看别人的) ...

  9. iOS BCD码、数据流、字节和MD5计算

    一.各个之间的相互转换 1.字符串转数据流NSData NSString *str = @"abc123"; NSData *dd = [str dataUsingEncoding ...

  10. python 求两个时间差

    def timeInterval(self): today = datetime.date.today() print today modifiedTime = os.stat(filename).s ...