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)\) — the permutation size and the number of moves. The second line contains \(n\) distinct integers, not exceeding \(n\) — the initial permutation. Next \(m\) lines each contain two integers: the \(i\)-th line contains integers \(a_{i}\) and \(b_{i}\) \((1\leq a_{i},b_{i}\leq n,a_{i}\neq b_{i})\) — the positions of elements that were changed during the \(i\)-th move.

输出格式:

In the only line print a single real number — the answer to the problem. The answer will be considered correct if its relative or absolute error does not exceed \(10^{-6}\).

输入输出样例

输入样例#1:

2 1
1 2
1 2

输出样例#1:

0.500000000

输入样例#2:

4 3
1 3 2 4
1 2
2 3
1 4

输出样例#2:

3.000000000

思路

这道题真的水。 --Mercury

完全想不到的状态设计,感谢\(Mercury\)巨佬的指点。

定义\(f(i,j)\)为位置\(i\)上的数比位置\(j\)上的数大的概率。假设每次交换都是\(100\%\)成功的,不妨设这次交换的数的下标为\(a,b\),那么对于任意的\(f(i,a),f(i,b)\)就要被交换,\(f(a,i),f(b,i)\)也要被交换。可是当前交换的概率是\(50\%\)的,所以\(f(i,a),f(i,b)\)之间的差值要被分别减少\(50\%\),也就相当于\(f(i,a)=f(i,b)=(f(i,a)+f(i,b))\div 2\)。同理,\(f(a,i)=f(b,i)=(f(a,i)+f(b,i))\div 2\)。最后的逆序对期望,也就是\(\Sigma [i<j]f(i,j)\times 1\),也就是\(\Sigma [i<j]f(i,j)\)。

还要再胡扯两句。 其实只要想出了\(f(i,j)\)这个东西,什么都简单了,可是又会有几个人能够想到这种方法呢?完全没有类似的情况作为参考,掌握了这道题却又能给类似的题提供经验(毕竟也没有类似的题)。下一次见到了这种思维量大的题,还是不太能想得出。思维的活跃在\(OI\)中还是有很大的作用的啊!

AC代码

#include<bits/stdc++.h>
#define RG register
using namespace std;
int n,m,a[1005];
double ans,f[1005][1005];
int read()
{
    RG int re=0;RG char ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)) re=(re<<3)+(re<<1)+ch-'0',ch=getchar();
    return re;
}
int main()
{
    n=read(),m=read();
    for(RG int i=1;i<=n;i++) a[i]=read();
    for(RG int i=1;i<=n;i++)
        for(RG int j=i+1;j<=n;j++)
            if(a[i]>a[j]) f[i][j]=1.0;
            else f[j][i]=1.0;
    while(m--)
    {
        RG int x=read(),y=read();
        if(x==y) continue;
        for(RG int i=1;i<=n;i++)
        {
            if(i==x||i==y) continue;
            f[i][x]=f[i][y]=(f[i][x]+f[i][y])/2;
            f[x][i]=f[y][i]=(f[x][i]+f[y][i])/2;
        }
        f[x][y]=f[y][x]=0.5;
    }
    for(RG int i=1;i<=n;i++)
        for(RG int j=i+1;j<=n;j++)
            ans+=f[i][j];
    printf("%.8f",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/coder-Uranus/p/9899145.html

时间: 2024-10-30 07:19:05

CodeForces 258D Little Elephant and Broken Sorting(期望)的相关文章

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

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)\)

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 - 204C Little Elephant and Furik and Rubik

CodeForces - 204C Little Elephant and Furik and Rubik 个人感觉是很好的一道题 这道题乍一看我们无从下手,那我们就先想想怎么打暴力 暴力还不简单?枚举所有字串,再枚举所有位置,算出所有答案不就行了 我们自然不能无脑暴力,但是暴力可以给我们启发 我们知道所有对答案做出贡献的字符一定是相同的(废话) 所以我们可以O(n^2)首先枚举两个字符串中相同的字符然后再考虑如何贡献 然后计算出所有的方案下的值,再除以n*(n+1)*(2*n+1)/6 [不知

Codeforces 220B - Little Elephant and Array 离线树状数组

This problem can be solve in simpler O(NsqrtN) solution, but I will describe O(NlogN) one. We will solve this problem in offline. For each x (0?≤?x?<?n) we should keep all the queries that end in x. Iterate that x from 0 to n?-?1. Also we need to kee

CodeForces 258B Little Elephant and Elections 数位DP

前面先用数位DP预处理,然后暴力计算组合方式即可. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include

CodeForces 221D Little Elephant and Array

Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 221D64-bit integer IO format: %I64d      Java class name: (Any) The Little Elephant loves playing with arrays. He has array a,

CodeForces 259A Little Elephant and Chess

Little Elephant and Chess Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 259A Description The Little Elephant loves chess very much. One day the Little Elephant and his friend decided t

Codeforces - 1264C - Beautiful Mirrors with queries - 概率期望dp

一道挺难的概率期望dp,花了很长时间才学会div2的E怎么做,但这道题是另一种设法. https://codeforces.com/contest/1264/problem/C 要设为 \(dp_i\) 表示第 \(i\) 个格子期望经过多少次,所以 \(dp_{n+1}=1\). https://www.cnblogs.com/suncongbo/p/11996219.html 原文地址:https://www.cnblogs.com/KisekiPurin2019/p/12063633.ht