传送门——Codeforces

传送门——Atcoder


考虑逆序对的产生条件,是存在两个数\(i,j\)满足\(i < j,a_i > a_j\)

故设\(dp_{i,j}\)表示\(a_i>a_j\)的概率,每一次一个交换操作时\(O(n)\)地更新即可。

AGC030D就在模意义下运算,最后就乘上\(2^Q\)就行了

看着好简单啊就是想不到

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cctype>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<vector>
#include<cmath>
#define ld long double
//This code is written by Itst
using namespace std; inline int read(){
int a = 0;
char c = getchar();
bool f = 0;
while(!isdigit(c) && c != EOF){
if(c == '-')
f = 1;
c = getchar();
}
if(c == EOF)
exit(0);
while(isdigit(c)){
a = a * 10 + c - 48;
c = getchar();
}
return f ? -a : a;
} ld dp[1010][1010];
int num[1010] , N , M; int main(){
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
//freopen("out","w",stdout);
#endif
N = read();
M = read();
for(int i = 1 ; i <= N ; ++i)
num[i] = read();
for(int i = 1 ; i <= N ; ++i)
for(int j = i - 1 ; j ; --j){
dp[i][j] = num[i] > num[j];
dp[j][i] = num[j] > num[i];
}
for(int i = 1 ; i <= M ; ++i){
int a = read() , b = read();
for(int j = 1 ; j <= N ; ++j)
if(j != a && j != b){
dp[j][a] = dp[j][b] = (dp[j][a] + dp[j][b]) * 0.5;
dp[a][j] = dp[b][j] = (dp[a][j] + dp[b][j]) * 0.5;
}
dp[a][b] = dp[b][a] = (dp[a][b] + dp[b][a]) * 0.5;
}
ld sum = 0;
for(int i = 1 ; i <= N ; ++i)
for(int j = i - 1 ; j ; --j)
sum += dp[j][i];
cout << fixed << setprecision(8) << sum;
return 0;
}

CF258D Little Elephant and Broken Sorting/AGC030D Inversion Sum 期望、DP的更多相关文章

  1. CF258D Little Elephant and Broken Sorting (带技巧的DP)

    题面 \(solution:\) 这道题主要难在考场上能否想到这个思路(即如何设置状态)(像我这样的蒟蒻就想不到呀QAQ)不过这一题确实很神奇! \(f[i][j]:\)表示第 \(a_i\) 个数比 ...

  2. CodeForces 258D Little Elephant and Broken Sorting(期望)

    CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\ ...

  3. Codeforces 258D Little Elephant and Broken Sorting (看题解) 概率dp

    Little Elephant and Broken Sorting 怎么感觉这个状态好难想到啊.. dp[ i ][ j ]表示第 i 个数字比第 j 个数字大的概率.转移好像比较显然. #incl ...

  4. CF 258 D. Little Elephant and Broken Sorting

    D. Little Elephant and Broken Sorting 链接 题意: 长度为n的序列,m次操作,每次交换两个位置,每次操作的概率为$\frac{1}{2}$,求m此操作后逆序对的期 ...

  5. CodeForces - 258D Little Elephant and Broken Sorting

    Discription The Little Elephant loves permutations of integers from 1 to n very much. But most of al ...

  6. CodeForces - 258D:Little Elephant and Broken Sorting(概率DP)

    题意:长度为n的排列,m次交换xi, yi,每个交换x,y有50%的概率不发生,问逆序数的期望  .n, m <= 1000 思路:我们只用维护大小关系,dp[i][j]表示位置i的数比位置j的 ...

  7. 「AGC030D」Inversion Sum

    「AGC030D」Inversion Sum 传送门 妙啊. 由于逆序对的个数最多只有 \(O(n^2)\) 对,而对于每一个询问与其相关的逆序对数也最多只有 \(O(n)\) 对,我们可以对于每一对 ...

  8. 【AGC030D】Inversion Sum DP

    题目大意 有一个序列 \(a_1,a_2,\ldots,a_n\),有 \(q\) 次操作,每次操作给你两个数 \(x,y\),你可以交换 \(a_x,a_y\),或者什么都不做. 问你所有 \(2^ ...

  9. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

随机推荐

  1. VUE基于ElementUI搭建的简易单页后台

    一.项目链接 GitHub 地址: https://github.com/imxiaoer/ElementUIAdmin 项目演示地址:https://imxiaoer.github.io/Eleme ...

  2. Ansible--inventory

    简介 Inventory 是 Ansible 管理主机信息的配置文件,相当于系统 HOSTS 文件的功能,默认存放在 /etc/ansible/hosts.为方便批量管理主机,便捷使用其中的主机分组, ...

  3. Sublime Text3 安装 markdownediting插件 报错 Error loading syntax file "Packages/Markdown/Markdown.tmLanguage":

    问题: Error loading syntax file "Packages/Markdown/Markdown.sublime-syntax": 解决方法: ./Data/Lo ...

  4. 简述 Spring Cloud 是什么1

    很多同学都了解了Spring ,了解了 Spring Boot, 但对于 Spring Cloud 是什么还是比较懵逼的. 本文带你简单的了解下,什么是Spring Cloud. Spring Clo ...

  5. python 3.3.2报错:No module named 'urllib2'

    ModuleNotFoundError: No module named 'urllib3' 1. ImportError: No module named 'cookielib'1 Python3中 ...

  6. [VMWARE] [CENTOS7] 安装VMware-Tools

    安装VM-Tools 先使用yum install 安装Perl与net-tools# sudo yum install net-tools# sudo yum install perl 加载CDRO ...

  7. 第六章 键盘(SYSMETS4)

    //SYSMETS.H -- System metrics display structure #include <Windows.h> #define NUMLINES ((int) ( ...

  8. zabbix监控磁盘IO

    我这里有两种方法,感觉都不错.我这里主要是写一下监控的脚本. 1.使用iostat命令监控 1)首先打开配置文件的自定义脚本功能,然后编写脚本. #!/bin/bash ];then echo &qu ...

  9. Linux初学 - Centos7忘记root密码的解决办法

    开机进入启动界面后,要按照屏幕的下方的操作提示迅速按下“e”键. 按下“e”键后即来到启动文件界面,这时按键盘上面的方向键“下”,一直到文件底部,在"LANG=zh_cn.UTF-8&quo ...

  10. C++中的istringstream

    istringstream用于执行C++风格的串流操作. 下面的示例是使用一个字符串初始化istringstream类,然后再使用>>操作符来依次输出字符串中的内容. temp_mon=& ...