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 ...
随机推荐
- python经典一百道习题(转自奶酪博客)
无论学习哪门计算机语言,只要把100例中绝大部分题目都做一遍,就基本掌握该语言的语法了. [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? #Filena ...
- 提升 Web开发性能的 10 个技巧
随着网络的高速发展,网络性能的持续提高成为能否在芸芸App中脱颖而出的关键.高度联结的世界意味着用户对网络体验提出了更严苛的要求.假如你的网站不能做到快速响应,又或你的App存在延迟,用户很快就会移情 ...
- Android 如何通过Retrofit提交Json格式数据
本文将介绍如何通过retrofit库post一串json格式的数据.首先post的json数据格式如下: { "Id": "string", "Dev ...
- 4G牌照影响
与3G牌照发放整整讨论了10年不同,4G牌照发放在2009年3G规模建设4年后就进行了发放,也颇匹配于行业的加速度.那么,4G到底会在哪些方面.在何种程度上改变中国呢?其实,4G的影响可能没有那么大, ...
- 解决常见SVN冲突问题(转)
转自:http://www.w3cfuns.com/blog-5443287-5403523.html 一个大项目在开发中可能会拆分成几个小项目,分别分去,同时共通的部分再由人做,做完后再统一合并.同 ...
- PHP 常用的无限遍历方法
一.根据父节点ID循环遍历其下所有子节点 /** * @name 根据id递归某个父类节点下的所有分类节点 * @author tbj * @date 2015-12-19 */ public fun ...
- C#调用Lame.exe
string lameEXE = @"D:\lame3.100\lame.exe"; string lameArgs = "-b 128"; string wa ...
- 如何让Sublime Text编辑器支持新的ABAP关键字
ABAP 7.40推出了许多新的关键字 - keyword,您的Sublime Text可能无法针对这些新关键字实现期望的语法高亮显示(syntax highlight)或者自动完成(auto com ...
- iPhone Tutorials
http://www.raywenderlich.com/tutorials This site contains a ton of fun written tutorials – so many t ...
- HDU 6166 Senior Pan(多校第九场 二进制分组最短路)
题意:给出n个点和m条有向边(有向边!!!!我还以为是无向查了半天),然后给出K个点,问这k个点中最近的两点的距离 思路:比赛时以为有询问,就直接丢了,然后这题感觉思路很棒,加入把所有点分成起点和终点 ...