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 ...
随机推荐
- phpMyAdmin安装
phpMyAdmin是MySql的一个Web操作界面. phpMyAdmin官网貌似被和谐了,经常无法访问.不过我们可以从GitHub下载phpMyAdmin. 然后解压,搭建与普通的PHP网站一样. ...
- LotusPhp起步:经典的HelloWorld
写了几篇LotusPhp,一直没有跑个程序,感觉好像步骤有点错,所以先上个经典的Demo,HelloWorld吧 先按推荐目录建好文件夹,如果懒的建,下面有下载的Demo包,解压就可以用,因为简单,也 ...
- mysql报错Table '.\erchina_news\v9_search' is marked as crashed and should be repaired
直切正题 报该问题的是表引导坏了,需要修复表就行 方法一: 找到mysql的安装目录的bin/myisamchk工具,在命令行中输入: myisamchk -c -r ../data/erchina_ ...
- C# 基础 计算平均值的方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- iOS反射机制
iOS属性反射:说白了,就是将两个对象的所有属性,用动态的方式取出来,并根据属性名,自动绑值.(注意:对象的类,如果是派生类,就得靠其他方式来实现了,因为得到不该基类的属性.) 本人常用的反射方式,有 ...
- django-url调度器-中级篇
在初级篇中,我们接触了: 1.url 的简单编写 2.两种传参的方式 3.捕获的参数总是字符串 4.为视图设置默认参数 …… 在中级篇中将更进一步. 包含其它的URLconfs 当网站非常大的时候,将 ...
- Moses更改权重的命令变化 -d -t -
-l 可以用: weight-l 或者lm (不需要在前面加-) 还是用-weight-overwrite “Distortion0= 0"更保险 reording weight i ...
- 统计工具之QQ图
正态 QQ 图和普通 QQ 图 分位数-分位数 (QQ) 图是两种分布的分位数相对彼此进行绘制的图.评估数据集是否正态分布,并分别研究两个数据集是否具有相似的分布. 如何构建正态 QQ 图 首先,数据 ...
- [目录]Pentaho Kettle解决方案:使用PDI构建开源ETL解决方案
第一部分:开始 1 ETL入门 1.1 OLTP和数据仓库对比 1.2 ETL是什么 1.2.1 ETL解决方案的演化过程 1.2.2 ET ...
- 基于HTML5的可预览多图片Ajax上传
一.关于图片上传什么什么的 在XHTML的时代,我们使用HTML file控件上传图片一次只能上传一张.要一次上传多图,做法是借助于flash.例如swfupload.js.可惜,使用复杂的点,比如f ...