考虑记\(f_{i,j,k}\)为\(k\)次操作后,\(i,j\)位置被调换的概率。

那么我们考虑枚举我们要算的答案即\((x,y)\)。

那么有\(\frac{n * (n + 1)}{2}\)种调换顺序。

以此分类讨论:

一:不相交:

对答案不产生影响。

二:包含



因为是反转操作,考虑枚举枚举翻转移动的距离,从\(f_{i + q,j + q,k - 1}\)转移过来。

三:端点相交

同样考虑枚举反转距离 ,从\(f_{i + q,j,k - 1}\)还有\(f_{i,j + q,k - 1}\)。

利用前缀和可以做到\(O(k n^2)\)。

由于是实数运算,所以在\(k\)增大的过程中,\(\Delta ans\to 0\),所以我们取一个数据范围能够容忍的大数\(k\)作为答案,实测\(k = 900\)效果很不错。

CF513G3 Inversions problem
// code by fhq_treap
#include<bits/stdc++.h>
#define ll int
#define N 200 inline ll read(){
char C=getchar();
ll A=0 , F=1;
while(('0' > C || C > '9') && (C != '-')) C=getchar();
if(C == '-') F=-1 , C=getchar();
while('0' <= C && C <= '9') A=(A << 1)+(A << 3)+(C - 48) , C=getchar();
return A*F;
} ll n,k;
ll num[N];
double dp[N][N],tmp[N][N]; double calc(int x){return (double)(x) * (double)(x + 1) / 2;} int main(){
scanf("%d%d",&n,&k);
for(int i = 1;i <= n;++i)
scanf("%d",&num[i]);
// for(int i = 1;i <= n;++i)
// for(int j = i + 1;j <= n;++j)
// dp[i][j] = 1.0;
k = std::min((ll)900,k);
double tot = (n + 1) * n / 2;
for(int m = 1;m <= k;++m){
for(int i = 1;i <= n;++i)
for(int j = 1;j <= n;++j)
tmp[i][j] = 0;
for(int i = 1;i <= n;++i)
for(int j = i + 1;j <= n;++j){
tmp[i][j] = dp[i][j] * (calc(i - 1) + calc(j - i - 1) + calc(n - j)) / tot;
// std::cout<<i<<" "<<j<<" "<<tmp[i][j]<<" "<<(calc(i - 1) + calc(j - i + 1) + calc(n - j))<<std::endl;
//i,j
for(int q = 1 - i;q + j <= n;++q)
tmp[i][j] += (1 - dp[i + q][j + q]) * std::min(std::min(i,i + q),n - std::max(j,j + q) + 1) / tot;
// std::cout<<i<<" "<<j<<" "<<tmp[i][j]<<std::endl;
//i
for(int q = 1 - i;q < j - i;++q)
tmp[i][j] += dp[i + q][j] * std::min(std::min(i,i + q),j - std::max(i,i + q)) / tot;
// std::cout<<i<<" "<<j<<" "<<tmp[i][j]<<std::endl;
//j
for(int q = i - j + 1;q + j <= n;++q)
tmp[i][j] += dp[i][j + q] * std::min(std::min(j,j + q) - i,n - std::max(j + q,j) + 1) / tot;
// std::cout<<i<<" "<<j<<" "<<tmp[i][j]<<std::endl;
}
std::memcpy(dp,tmp,sizeof(tmp));
// for(int i = 1;i <= n + 1;++i,puts(""))
// for(int j = i + 1;j <= n;++j)
// std::cout<<dp[i][j]<<' ';
}
double ans = 0.0;
for(int i = 1;i <= n;++i)
for(int j = i + 1;j <= n;++j){
if(num[i] < num[j])ans += dp[i][j];
else
ans += 1 - dp[i][j];
}
std::printf("%.10f",ans);
}

CF513G3 Inversions problem的更多相关文章

  1. Codeforces 513G1 513G2 Inversions problem [概率dp]

    转自九野:http://blog.csdn.net/qq574857122/article/details/43643135 题目链接:点击打开链接 题意: 给定n ,k 下面n个数表示有一个n的排列 ...

  2. [UCSD白板题] Number of Inversions

    Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...

  3. Rockethon 2015

    A Game题意:A,B各自拥有两堆石子,数目分别为n1, n2,每次至少取1个,最多分别取k1,k2个, A先取,最后谁会赢. 分析:显然每次取一个是最优的,n1 > n2时,先手赢. 代码: ...

  4. Codeforces Round Rockethon 2015

    A. Game 题目大意:A有N1个球,B有N2个球,A每次可以扔1-K1个球,B每次可以扔1-K2个球,谁先不能操作谁熟 思路:.....显然每次扔一个球最优.... #include<ios ...

  5. 《算法导论》Problem 2-4 Inversions

    在Merge Sort的基础上改改就好了. public class Inversions { public static int inversions(int [] A,int p, int r) ...

  6. Dynamic Inversions II 逆序数的性质 树状数组求逆序数

    Dynamic Inversions II Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Other ...

  7. Dynamic Inversions 50个树状数组

    Dynamic Inversions Time Limit: 30000/15000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others ...

  8. [Swift]LeetCode775. 全局倒置与局部倒置 | Global and Local Inversions

    We have some permutation Aof [0, 1, ..., N - 1], where N is the length of A. The number of (global) ...

  9. [LeetCode] Global and Local Inversions 全局与局部的倒置

    We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. The number of (global) ...

随机推荐

  1. NX Open 切削层加载

    UF_CUT_LEVELS_load Loads the current cut levels for the specified operation into the data structure ...

  2. C++ 与 Visual Studio 2019 和 WSL(三)

    头文件 如果不小心修改了 Linux C/C++ 标准头文件,可以下面这样操作进行恢复: 项目 → 重新扫描解决方案

  3. Perl操作excel2007的模块

    详细版:https://www.jianshu.com/p/84bda53827c8 第一种方法: 读写excel2007文档的perl模块: Spreadsheet::XLSX(读)和Spreads ...

  4. 深入理解java中main方法

    理解main方法语法 深入理解main方法: 解释main方法的形式:public static void main(String[] args){} main方法调用者:虚拟机 java虚拟机需要调 ...

  5. 初学Python-day11 函数4

    函数 1.递归函数 自己不断调用自己的过程 2.递归求和 1 def sum(arg): 2 if arg == 1: 3 return 1 4 return arg + sum(arg - 1) 5 ...

  6. 【UE4 C++ 基础知识】<13> 多线程——TaskGraph

    概述 TaskGraph 系统是UE4一套抽象的异步任务处理系统 TaskGraph 可以看作一种"基于任务的并行编程"设计思想下的实现 通过TaskGraph ,可以创建任意多线 ...

  7. HTML中的emment

    emment的优点和使用方法: 优点:Emment是前端开发者必备的工具,使用它可以大大提高前端开发效率. 使用方法:Emment的使用方法非常简单,直接在编辑器上输入HTML和CSS代码的缩写,然后 ...

  8. Spring Security中配置AccessDeniedHandler没有生效

    现象 在 WebSecurityConfigurerAdapter 配置了如下代码: // 自定义未授权和未登录异常 http.exceptionHandling() .accessDeniedHan ...

  9. Scrum Meeting 16

    第16次例会报告 日期:2021年06月11日 会议主要内容概述: 最后一次例会 一.进度情况 我们采用日报的形式记录每个人的具体进度,链接Home · Wiki,如下记录仅为保证公开性: 组员 负责 ...

  10. sql_exporter的使用

    sql_exporter的使用 一.背景 二.sql-exporter的使用 1.下载 2.配置文件 1.sql_exporter.yml 2.collectors 目录中的配置文件 1.collec ...