Codeforces 258d little elephant and broken sorting (expected)

Source: Internet
Author: User

Cf258d little elephant and broken sorting

Question Translation

There is an \ (1 \ SIM n \) arrangement, the \ (M \) operation is performed, the operation is exchange \ (a, B \). Each operation has a probability of \ (50 \ %.

Evaluate the number of expected Reverse Order pairs after performing the \ (M \) operation.

\ (N, m \ le 1000 \)

Input/output format:

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.

Output Format:

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

Input and Output sample input sample #1:
2 11 21 2
Output sample #1:
0.500000000
Input example #2:
4 31 3 2 41 22 31 4
Output sample #2:
3.000000000
Ideas

This question is true. -- Mercury

I would like to thank the mercury for his unexpected status design.

Define \ (f (I, j) \) as the probability that the ratio of numbers on the position \ (I \) is greater than that on the position \ (J. Assuming that each exchange is successful \ (100 \ % \), you may set the subscript of the number of this exchange as \ (a, B \), then any \ (f (I, A), F (I, B) \) will be exchanged, \ (f (a, I), F (B, I) \) will also be exchanged. However, the probability of the current switch is \ (50 \ % \), SO \ (f (I, A), F (I, B )\) the difference between them is reduced by \ (50 \ % \), which is equivalent to \ (f (I, a) = f (I, B) = (f (I,) + f (I, B) \ Div 2 \). Similarly, \ (f (a, I) = f (B, I) = (f (a, I) + f (B, I) \ Div 2 \). The final backward direction is expected, that is, \ (\ Sigma [I <j] f (I, j) \ times 1 \), that is \ (\ Sigma [I <j] f (I, j )\).

Let's say a few more things.In fact, as long as we come up with \ (f (I, j) \), everything is simple. But how many people can think of this method? There is no similar situation as a reference. mastering this question can provide experience for similar questions (after all, there is no similar question ). The next time I saw this question with a large amount of thinking, I still cannot figure it out. The active thinking in \ (OI \) still plays a major role!

AC code
#include<bits/stdc++.h>#define RG registerusing 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;}

Codeforces 258d little elephant and broken sorting (expected)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.