"Codeforces 749E" inversions after Shuffle

Source: Internet
Author: User
Tags shuffle

Http://codeforces.com/problemset/problem/749/E (Topic link)

Test instructions

The arrangement of a 1~n is given, and a continuous segment is selected from the same probability, and the length is set to L. The total order of successive segments is re-arranged, and the expected number of reverse pairs of the whole original sequence is obtained.

Solution

Consider the contribution for each logarithmic ${(a_i,a_j), i<j}$ calculation.

1. Continuous segment contains ${a_i,a_j}$

It may be possible to set ${a_i<a_j}$, only after the arrangement ${a_j}$ again ${a_i}$ before the answer will contribute (${a_i>a_j}$ the same situation), the continuous segment length is ${l}$.

Thus satisfies the ${a_i}$ in the ${a_j}$ front the permutation number is ${p_l^{l-2}}$, the probability: ${\frac{p_l^{l-2}}{p_l^l}=\frac{1}{2}}$.

A continuous segment containing ${a_i}$ and ${a_j}$ has ${i* (n-j+1)}$, with the probability that ${\frac{2*i* (n-j+1)}{n* (n+1)}}$.

So its expectation equals two probabilities multiplied:

$${q_{i,j}=\frac{i* (n-j+1)}{n* (n+1)}}$$

This is ${o (n^2)}$, consider optimization. Total expectation:

$${q=\sum_{i=1}^n \sum_{j=i+1}^n q_{i,j}}$$

$${q=\sum_{i=1}^n \sum_{j=i+1}^n \frac{i* (n-j+1)}{n* (n+1)}}$$

The discovery of ${(n-j+1)}$ was continuous and became:

$${q=\sum_{i=1}^n \frac {i* (n-i) * (n-i+1)} {2*n* (n+1)}}$$

This complexity is ${o (n)}$.

2. Continuous segment does not contain ${a_i,a_j}$

If ${a_i<a_j}$, they will not have the opportunity to change the relative position because they are not included in the successive segments, so they will not contribute to the answer. Consider the situation of ${a_i>a_j}$.

Then the range that can be taken by the successive segments is: ${[1,j-1],[i+1,n]}$. Considering the interval ${[i+1,j-1]}$ is calculated 2 times, the probability of the interval:

$${p=\frac {(j-1) *j+ (n-i) * (n-i+1)-(j-i-1) * (I-J)} {N (n+1)}}$$

And then it looks like you can do it with a tree-like array, first pit. Send a ${o (n^2)}$ code.

Detail code
Codeforces 749e#include<algorithm> #include <iostream> #include <cstdlib> #include <cstring > #include <cstdio> #include <cmath> #define LL long long#define inf 1<<30#define Pi ACOs (-1.0) # Define free (a) freopen (a ".", "R", stdin), Freopen (a ". Out", "w", stdout); using namespace Std;const int maxn=100010; LL c[maxn],s[maxn];int N,A[MAXN];d ouble ans;int Main () {scanf ("%d", &n), for (int i=1;i<=n;i++) scanf ("%d", &a [i]); for (int i=1;i<=n;i++) ans+= (double) (i* (n-i) * (n-i+1))/(2*n* (n+1)), for (Int. i=1;i<=n;i++) for (int j=i+1;j<=n; J + +) if (A[i]>a[j]) ans+= (double) ((j-1) *j+ (n-i) * (n-i+1)-(j-i-1) * (j-i))/(n (n+1));p rintf ("%.12lf", ans); return 0;}

"Codeforces 749E" inversions after Shuffle

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.