Codeforces 351B Jeff and Furik 概率 | DP
B. 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 permutationp. 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.
题意: 两个人轮流游戏,先手交换相邻两个数,后手先抛硬币,正面,就随机找到一对相邻左大右小的数换,反面,就随机找到一对相邻右大左小的数换,直到整个数列升序排列,求最小期望步数。
思路: 由于第一个人每次都会减少一对逆序对,而后手会50%减少一对,50%增加一对,我们把两个人凑起来就是:
50%逆序对不变,50%减少2对
令dp[i]表示减少i个逆序对的步数的期望
dp[i]=dp[i]*0.5+dp[i-2]*0.5+2(要减少i个逆序对期望dp[i]步 = 此次两人共2步+减少dp[i-2]逆序对的期望步数+减少dp[i]逆序対的期望步数)
算出来就是dp[i]=dp[i-2]+4,初值:dp[0]=0,dp[1]=1
故dp数组分奇偶后就是两个等差数列,dp[i]=2*i(i为偶数),dp[i]=2*i-1(i为奇数),逆序对直接n^2暴力即可
#include "bits/stdc++.h"
using namespace std;
#define rep(i, s, n) for(int i=s;i<n;i++)
const int N=;
int a[N];
int main() {
int n;
while(cin >> n){
rep(i, , n) cin >> a[i];
int s = ;
rep(i, , n) rep(j, i + , n) if (a[i] > a[j]) s++;
double ans = s & ? s / * + : s * ;
printf("%.6f\n", ans);
}
return ;
}
最新文章
- supervisor的安装与简单介绍
- java中方法的重写与重载的区别
- RESTFUL API 安全设计指南
- 计算机网络(10)-----TCP的拥塞控制
- 对jQuery选择器的总结
- linux 安装vpn
- Apache使用mysql认证用户
- Xcode 修改工程名称
- Oracle中字段的修改操作语法
- MIPS 跳转指令BAL vs JAL
- 1179: [Apio2009]Atm
- js实现换肤效果
- Anaconda+linux +opencv+dlib安装
- Java String:重要到别人只能当老二的字符串类
- pymysql模块
- 遍历List过程中同时修改
- Android_编程开发规范
- yocto-sumo源码解析(四):bitbake
- 006.FTP用户访问控制配置
- phpstrom 配置xdebug在vagrant上调试