Codeforces Round #204 (Div. 2)
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 iand 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.
就是现在给出一个1~n的排列, Jeff和Furik分别轮流进行操作, Jeff先手, Jeff会选择相邻的两个数p[i], p[i + 1]交换位置, 然后轮到Furik, Furiki每次都会抛一个硬币, 出现正面就在序列中选取相邻的满足p[i] > p[i + 1]的两个数交换, 出现反面则选取任意一个相邻的满足p[i] < p[i + 1]的一对数进行交换, 操作时当这个序列变成递增序列的时候操作结束, 假设Jeff每一步都操作都最优(使得接下来剩余的操作次数最少)那么问一共需要的操作步数的期望是多少
其实这个期望就是逆序对数乘2?但是提交了并不对,所以是不是我想的不对啊, Jeff肯定会让逆序数-1,但是Furik是有0.5的可能让逆序数减1,所以如果逆序数是奇数的话,最后一次是Jeff啊,所以需要减1的,但是偶数就不用了
#include <bits/stdc++.h>
using namespace std;
int c[];
int n;
int lowbit(int i)
{
return i&(-i);
}
int insert(int i,int x)
{
while(i<=n)
{
c[i]+=x;
i+=lowbit(i);
}
return ;
} int getsum(int i)
{
int sum=;
while(i>)
{
sum+=c[i];
i-=lowbit(i);
}
return sum;
}
int main()
{
while(cin>>n)
{
int ans=;
memset(c,,sizeof(c));
for(int i=; i<=n; i++)
{
int a;
cin>>a;
insert(a,);
ans+=i-getsum(a);
}
if (ans&) cout<<*ans-<<endl;
else cout<<*ans<<endl;
}
return ;
}
Codeforces Round #204 (Div. 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(取整之后数的和-原来数的和) 先使所有的 ...
- 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) B. Jeff and Furik
http://codeforces.com/contest/351/problem/B 题意: 给出一个n的排列 第一个人任选两个相邻数交换位置 第二个人有一半的概率交换相邻的第一个数>第二个数 ...
- 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) A.Jeff and Digits
因为数字只含有5或0,如果要被90整除的话必须含有0,否则输出-1 如果含有0的话,就只需考虑组合的数字之和是9的倍数,只需要看最大的5的个数能否被9整数 #include <iostream& ...
- 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 ...
- 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) C
写了一记忆化 TLE了 把double换成long long就过了 double 这么耗时间啊 #include <iostream> #include<cstdio> #i ...
- Codeforces Round #204 (Div. 2): B
很简单的一个题: 只需要将他们排一下序,然后判断一下就可以了! 代码: #include<cstdio> #include<algorithm> #define maxn 10 ...
随机推荐
- SpringBoot整合国际化I18n
本文主要实现的功能: 从文件夹中直接加载多个国际化文件 后台设置前端页面显示国际化信息的文件 实现 国际化项目初始化,简单看下项目的目录和文件 在resource下创建国际化文件 messages.p ...
- ubuntu中wine下安装QQ
原文:http://jingyan.baidu.com/article/359911f55da27057fe0306d8.html 可以把win改成最新版
- webpack入门之最简单的例子 webpack4
webpack在目前来说应该是前端用的比较多的打包工具了,那么对于之前没有接触过这块的该怎么办呢?答案很明显嘛,看资料,查文档,自己去琢磨,自己去敲一敲,跑一跑: 那么,这边我将以一个最基础的例子来将 ...
- MD5 介绍
MD5(单向散列算法)的全称是Message-Digest Algorithm 5(信息-摘要算法),经MD2.MD3和MD4发展而来.MD5算法的使用不需要支付任何版权费用. MD5功能: 输入任意 ...
- 最完整的台达PLC培训教程(沈阳工大)学习笔记1
1) 可编程控制器的应用1 开关量逻辑控制:电动机启动与停止2 运动控制:对步进电动机或伺服电动机的单轴或多轴系统实现位置控制3 过程控制:对温度.压力.流量等连续变化的模拟量进行闭环控制4 数据处理 ...
- ios invalid put policy encoding 七牛上传报错
获取七牛token的时候deadline不能为NSString类型 NSDictionary *infoDic = @{@"scope":@"yangtao", ...
- jquery插件serializeFormToObject
$.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() ...
- 爬虫_python3_requests_2
pip install requests 进行简单的操作 发送一个get请求 # 发送请求 import requests response = requests.get('http://httpbi ...
- Sublime Text 套件介紹(四):Pretty JSON
JSON,一個輕量級的資料交換語言,目前許多網站AJAX request的回應結果都是JSON格式 以下是一個標準的JSON格式 { "firstName": " ...
- kubernetes添加不了google apt-key
转自icepoint的博客 key来源 我的百度云盘 密码:v3wo 下载kube_apt_key.gpg到本地,上传到服务器后执行下面的命令 apt-get update && ap ...