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 ...
随机推荐
- Y2165终极分班考试题。
第一题答案:D 2.下面关于SQLServer中视图的说法错误的是:C 答案:视图是数据中存储的数据值得集合. 3.在JAVA中,关于日志记录工具log4j的描述错误的是:D 答案:log4j个输出级 ...
- vuex2 10分钟快速入门
因为太简单了,我直接就贴代码了~ #建立store.js import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex) export de ...
- ubuntu键盘映射
在sublime下开发习惯把CapsLock和Shift间交换,windows下有很多软件可以修改键盘映射,在ubuntu下可以是哦用xmodmap命令,使用方法如下: 在自己用户的home目录下新建 ...
- Java——HashSet和TreeSet的区别
HashSetHashSet有以下特点 不能保证元素的排列顺序,顺序有可能发生变化 不是同步的 集合元素可以是null,但只能放入一个null当向HashSet集合中存入一个元素时,HashSe ...
- Vue.js中data,props和computed数据
data data 是Vue实例的数据对象.Vue将会将data 的属性转换为 getter/setter, 也就是用Object.defineProperty方法(在官网里面有深入响应式原理里面具体 ...
- MySQL常用命令和语句
1.常用SQL语句 1)常用函数/*type可取值为:MICROSECONDSECONDMINUTEHOURDAYWEEKMONTHQUARTERYEARSECOND_MICROSECONDMINUT ...
- echarts getAttribute”的值: 对象为 null 或未定义 错误解决方法,
echarts.js引用放在head中或者放在body中HTML代码的前面了,造成加载时阻塞后面的html. 解决方法就是将echarts.js的引用放在</body>之前就可以了,完美解 ...
- WPF知识点全攻略02- WPF体系结构
WPF体系结构图: PersentationFramework.dll包含WPF顶层的类型,包括哪些表示窗口.面板以及其他类型控件的类型.他还实现了高层编程抽象,如样式.开发人员直接使用的大部分类都来 ...
- C++类型强制转换<转>
转载:http://www.cnblogs.com/goodhacker/archive/2011/07/20/2111996.html C风格的强制类型转换(Type Cast)很简单,不管什么类型 ...
- 美国司法部解禁guns打印技术
今日导读 你知道什么是 3D 打印吗?简单的说,只要有一张设计蓝图和适当的材料,就可以快速打印出实体物件.而最近据外媒报道,从今年 8 月 1 日起,在美国,拥有或公布枪支 3D 打印蓝图的行为都将属 ...