CF258D. Little Elephant and Broken Sorting

传送门

题意:

有一个1~n的排列,依次进行m次操作,第i次操作表示为\((x _i,y_i)\),交换以这两个值为下标的元素,每次操作有一半的概率成功,你需要求出最后序列的逆序对的期望个数.

分析:

因为逆序对是(x,y)的形式,那么考虑每一对(i,j)对答案的贡献.

\(f_{i,j}\)表示\(a_i>a_j\)的概率,我们发现每一个操作只影响\(O(n)\)个f值,于是可以处理出每一个f的初始值,每次更新受影响的值即可。

时间复杂度 \(O(NM)\), 空间复杂度 \(O(N^2)\).

int n,m,a[1005];
double ans,f[1005][1005];
int main(){
    n=read();m=read();
//对于长度为n的序列,有m次操作
    for(int i=1;i<=n;i++)
        a[i]=read();
//读入序列
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
        f[i][j]=a[i]>a[j];
//预处理出f数组的初始值
    for(int i=1;i<=m;i++){
        int x=read(),y=read();
        for(int j=1;j<=n;j++){
            f[x][j]=f[y][j]=(double)0.5*(f[x][j]+f[y][j]);
            f[j][x]=f[j][y]=(double)0.5*(f[j][x]+f[j][y]);
        }
//是否交换x,y的概率为0.5,
//则a[x]>a[j]和a[y]>a[j]的概率取决于它们的和.
//2式写成f[j][x]=f[j][y]=1-f[x][j]应该也可以.
        f[x][y]=f[y][x]=0.5;
//是否交换x,y的概率为0.5,
//所以a[x]>a[y]和a[y]>a[x]的概率都是0.5.
    }
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
            ans+=f[i][j];
//累加所有逆序对的概率
    printf("%.8lf",ans);
//答案保留到小数点后8位
    return 0;
}

原文地址:https://www.cnblogs.com/PPXppx/p/10328100.html

时间: 2024-11-03 07:39:37

CF258D. Little Elephant and Broken Sorting的相关文章

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

题面 \(solution:\) 这道题主要难在考场上能否想到这个思路(即如何设置状态)(像我这样的蒟蒻就想不到呀QAQ)不过这一题确实很神奇! \(f[i][j]:\)表示第 \(a_i\) 个数比第 \(a_j\) 个数大的几率,这样设置状态比较好转移:对于每一次 \(a_i\) 与 \(a_j\) 的交换,他只会影响到序列里,每一个数与\(a_i\),\(a_j\) 的胜率(一共有\(n\)次交换,只要每次交换复杂度在\(O(n)\)级别这道题就解决了了).而且我们不难发现转移时每一个数与

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

CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\(50\%\)的概率进行. 求进行\(m\)次操作以后的期望逆序对个数. \(n,m\le 1000\) 输入输出格式 输入格式: The first line contains two integers \(n\) and \(m\) \((1\leq n,m\leq 1000,n>1)\) -

CodeForces - 258D Little Elephant and Broken Sorting

Discription The Little Elephant loves permutations of integers from 1 to n very much. But most of all he loves sorting them. To sort a permutation, the Little Elephant repeatedly swaps some elements. As a result, he must receive a permutation 1,?2,?3

&lt;Dr.Elephant&gt;

Why Dr.Elephant? Most of Hadoop optimization tools out there, but they are focused on simplifying the deploy and managment of Hadoop clusters. Very few tools are designed to help Hadoop users optimize their flows. Dr.Elephant supports Hadoop with a v

HDU 5122 K.Bro Sorting(模拟——思维题详解)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubble sort will compare each pair of adjacent items and swap them if they are in the wrong o

ubnutu 安装sogoupinyin初学遇上 software database is broken 问题

ubuntu 16.04lts 安装的时候选择一路默认安装.也没有选择pinyin输入. 然后发现忘记安装输入法了. 于是下载了sogoupinyin linux  deb版本 双击安装.发现失败. 于是选择dpkg 方式安装.提示依赖包缺失问题.提示缺失 fcitx 等一堆包 然后 选择 apt-get 想下载这些包安装.但提示无法下载. 目测还是安装sogoupinyin缺失.于是陷入一个死循环.登录设置里面选择安装输入源.直接提示 software database is broken 看

CodeForces - 844C Sorting by Subsequences (排序+思维)

You are given a sequence a1,?a2,?...,?an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also wil

Section1.1 -- Broken Necklace

Broken Necklace You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, others blue, and others white, arranged at random. Here are two examples for n=29: 1 2 1 2 r b b r b r r b r b b b r r b r r r w r b r w w b b r r

Hadoop监控分析工具Dr.Elephant

公司基础架构这边想提取慢作业和获悉资源浪费的情况,所以装个dr elephant看看.LinkIn开源的系统,可以对基于yarn的mr和spark作业进行性能分析和调优建议. DRE大部分基于java开发,spark监控部分使用scala开发,使用play堆栈式框架.这是一个类似Python里面Django的框架,基于java?scala?没太细了解,直接下来就能用,需要java1.8以上. prerequest list: Java 1.8 PlayFramework+activator No