Codeforces Round #204 (Div. 2)->D. Jeff and Furik
1 second
256 megabytes
standard input
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.
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.
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.
2
1 2
0.000000
5
3 5 2 4 1
13.000000
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的更多相关文章
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) B. Jeff and Furik
http://codeforces.com/contest/351/problem/B 题意: 给出一个n的排列 第一个人任选两个相邻数交换位置 第二个人有一半的概率交换相邻的第一个数>第二个数 ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding
http://codeforces.com/problemset/problem/351/A 题意: 2*n个数,选n个数上取整,n个数下取整 最小化 abs(取整之后数的和-原来数的和) 先使所有的 ...
- 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 ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) E. Jeff and Permutation
http://codeforces.com/contest/351/problem/E 题意: 给出一些数,可以改变任意数的正负,使序列的逆序对数量最少 因为可以任意加负号,所以可以先把所有数看作正数 ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods
http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...
- 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 ...
- Codeforces Round #204 (Div. 2) A.Jeff and Digits
因为数字只含有5或0,如果要被90整除的话必须含有0,否则输出-1 如果含有0的话,就只需考虑组合的数字之和是9的倍数,只需要看最大的5的个数能否被9整数 #include <iostream& ...
- Codeforces Round #204 (Div. 2) C. Jeff and Rounding——数学规律
给予N*2个数字,改变其中的N个向上进位,N个向下进位,使最后得到得数与原来数的差的绝对值最小 考虑小数点后面的数字,如果这些数都非零,则就是 abs(原数小数部分相加-1*n), 多一个0 则 m ...
- Codeforces Round #204 (Div. 2)
D. Jeff and Furik time limit per test 1 second memory limit per test 256 megabytes input standard in ...
随机推荐
- 关于 mvc 中 连字符 - 和下划线 _转换的问题。
[潜水]大崔||哈尔滨(759666247) 10:02:16 如图 C#不承认 “-”[知府]古道今-湖北\xig<systemobject@126.com> 10:03:54 ...
- Keil的使用方法 - 常用功能(一)
Ⅰ.概述 学习一门软件的开发,开发工具的掌握可以说尤为重要.由于Keil集成开发工具支持多种MCU平台的开发,是市面上比较常见的,也是功能比较强大一款IDE.所以,对于大多数人说,选择Keil几乎是单 ...
- python笔记第二天
上节内容回顾和补充 编程语言 高级 低级 Python种类 JavaPython cPython ***** pypy 字节码 和 机器码 Python程序: 1. 终端: C:\python35\p ...
- Web Design:给实验室UI们的一堂课(下)
[讲稿]From top to down,自顶向下哈,首部栏.导航栏之后一般是页面的主模块,也就是Body部分,这一块儿才是你网站的核心内容,文章.新闻.动态.数据.图表.相册等都是在这儿体现出来.在 ...
- 微软CRM解决医药企业串货之痛
没有准确.及时的流向数据统计和分析,医药企业营销部门就无法有效管理串货泛滥问题,串货会造成渠道无利可赚,挫伤渠道的积极性,产品无人愿意卖,最终伤害的还是医药企业. 医药企业营销发展的不同阶段对串货的态 ...
- css权重及优先级问题
css权重及优先级问题 几个值的对比 初始值 指定值 计算值 应用值 CSS属性的 指定值 (specified value)会通过下面3种途径取得: 在当前文档的样式表中给这个属性赋的值,会被优先使 ...
- 浅谈HAL
参考:http://blog.csdn.net/mr_raptor/article/details/8074549 代码实现:http://blog.csdn.net/mr_raptor/articl ...
- 条款3:尽可能地使用const
如下为const修饰的几种类型: char name[] = "benxintuzi"; char* p1 = name; // non-const ...
- iTween基础之功能简介
一.iTween 介绍 .二.iTween 原理.三.iTween 下载.四.iTween 类介绍.五.主要功能介绍 原文地址:http://blog.csdn.net/dingkun520wy/ar ...
- shell基本语法备忘
1.第一行要写明shell种类 #!/bin/bash 2.打印输出 #!/bin/bashecho "Hello World !~" 3.变量定义 变量=前后不能有空格, ...