传送门——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. Tensorflow高效读取数据

    关于Tensorflow读取数据,官网给出了三种方法: 供给数据(Feeding): 在TensorFlow程序运行的每一步, 让Python代码来供给数据. 从文件读取数据: 在TensorFlow ...

  2. 使用apksigner对apk进行v2签名

    最近进行三方安全测试,剩最后一个问题: 原因是我用360加固宝之后,又用了360Signer对apk进行二次签名,而360Signer是用v1方式对apk进行签名的,所以安全检测还是不通过. 下面给出 ...

  3. 安卓开发之自定义一个view弹出框

    https://www.cnblogs.com/muyuge/p/6152167.html

  4. Java:【面向对象:抽象类,接口】

    本文内容: 抽象类 接口 抽象类与接口的异同 首发日期:2018-03-24 抽象类: 虽然已经有了父类,但有时候父类也是无法直接描述某些共有属性的,比如哺乳类和人类都会叫,而一般来说哺乳类这个父类并 ...

  5. [20180316]为什么不使用INDEX FULL SCAN (MIN/MAX).txt

    [20180316]为什么不使用INDEX FULL SCAN (MIN/MAX).txt --//链接:http://www.itpub.net/thread-2100456-1-1.html.自己 ...

  6. SonarQube 配置 LDAP(AD域)

    安装插件 1.下载 LDAP Plugin 插件,地址:https://docs.sonarqube.org/display/SONARQUBE67/LDAP+Plugin2.将下载的插件,放到 SO ...

  7. 百度-淘宝-360搜索引擎搜索API

    百度(baidu) Api地址:http://suggestion.baidu.com/su?wd=设计&p=3&cb=window.bdsug.sug window.bdsug.su ...

  8. Spring容器技术内幕之内部工作机制

    引言 Spring容器就像一台构造精妙的机器,我们通过配置文件向机器传达控制信息,机器就能够按照设定的模式工作.如果将Spring容器比作一辆车,那么可以将BeanFactory看成汽车的发动机,而A ...

  9. 前端性能优化成神之路--图片懒加载(lazyload image)

    图片懒加载(当然不仅限于图片,还可以有视频,flash)也是一种优化前端性能的方式.使用懒加载可以想要看图片时才加载图片,而不是一次性加载所有的图片,从而在一定程度从减少服务端的请求 什么是懒加载 懒 ...

  10. P1056 排座椅

    非原创 #include<bits/stdc++.h>using namespace std;int t1[2009];int t2[2009]; int findmax(int *a){ ...