Inversions After Shuffle
1 second
256 megabytes
standard input
standard output
You are given a permutation of integers from 1 to n. Exactly once you apply the following operation to this permutation: pick a random segment and shuffle its elements. Formally:
- Pick a random segment (continuous subsequence) from l to r. All
segments are equiprobable. - Let k = r - l + 1, i.e. the length of the chosen segment. Pick a random permutation of integers from 1 to k, p1, p2, ..., pk. All k!permutation are equiprobable.
- This permutation is applied to elements of the chosen segment, i.e. permutation a1, a2, ..., al - 1, al, al + 1, ..., ar - 1, ar, ar + 1, ..., an is transformed to a1, a2, ..., al - 1, al - 1 + p1, al - 1 + p2, ..., al - 1 + pk - 1, al - 1 + pk, ar + 1, ..., an.
Inversion if a pair of elements (not necessary neighbouring) with the wrong relative order. In other words, the number of inversion is equal to the number of pairs (i, j) such that i < j and ai > aj. Find the expected number of inversions after we apply exactly one operation mentioned above.
The first line contains a single integer n (1 ≤ n ≤ 100 000) — the length of the permutation.
The second line contains n distinct integers from 1 to n — elements of the permutation.
Print one real value — the expected number of inversions. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 9.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if
.
3
2 3 1
1.916666666666666666666666666667
分析:仔细分析可知答案为
,inv(l,r)代表[l,r]逆序数;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define intxt freopen("in.txt","r",stdin)
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline void umax(int &p,int q){if(p<q)p=q;}
inline void umin(int &p,int q){if(p>q)p=q;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t;
ll a[maxn],d[maxn];
double ans,now,pre;
void add(int x,int y)
{
for(int i=x;i<=n;i+=i&(-i))
d[i]+=y;
}
ll get(int x)
{
ll ret=;
for(int i=x;i;i-=i&(-i))
ret+=d[i];
return ret;
}
void init()
{
ll cnt=;
for(int i=;i<=n;i++)
{
cnt+=get(n)-get(a[i]);
add(a[i],);
}
for(int i=;i<=n;i++)
ans+=(n-i+)*((double)i*(i-)/+cnt);
memset(d,,sizeof(d));
}
int main()
{
int i,j;
scanf("%d",&n);
rep(i,,n)a[i]=read();
init();
rep(i,,n)
{
now=get(n)-get(a[i])+pre;
ans-=now;
pre=now;
add(a[i],i);
}
ans/=(double)n*(n+)/;
printf("%.10f\n",ans);
//system("Pause");
return ;
}
Inversions After Shuffle的更多相关文章
- 【codeforces 749E】 Inversions After Shuffle
http://codeforces.com/problemset/problem/749/E (题目链接) 题意 给出一个1~n的排列,从中等概率的选取一个连续段,设其长度为l.对连续段重新进行等概率 ...
- Codeforces 749E: Inversions After Shuffle
题目传送门:CF749E. 记一道傻逼计数题. 题意简述: 给一个 \(1\) 到 \(n\) 的排列,随机选取区间 \([l,r]\) 随机打乱区间内的元素,问打乱后的整个序列的逆序数期望. 题解: ...
- Inversions After Shuffle CodeForces - 749E (概率,期望)
大意: 给定一个$n$排列, 随机选一个区间, 求将区间随机重排后整个序列的逆序对期望. 考虑对区间$[l,r]$重排后逆序对的变化, 显然只有区间[l,r]内部会发生改变 而长为$k$的随机排列期望 ...
- Codeforces Round #388 (Div. 2)
# Name A Bachgold Problem standard input/output 1 s, 256 MB x6036 B Parallelogram is Back s ...
- Spark Shuffle原理、Shuffle操作问题解决和参数调优
摘要: 1 shuffle原理 1.1 mapreduce的shuffle原理 1.1.1 map task端操作 1.1.2 reduce task端操作 1.2 spark现在的SortShuff ...
- Collections.shuffle
1.Collections.shuffler 最近有个需求是生成十万级至百万级的所有随机数,最简单的思路是一个个生成,生成新的时候排重,但是这样时间复杂度是o(n^2),网上看了几个博客的解决方法都不 ...
- [LeetCode] Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- mapReduce的shuffle过程
http://www.jianshu.com/p/c97ff0ab5f49 总结shuffle 过程: map端的shuffle: (1)map端产生数据,放入内存buffer中: (2)buffer ...
- spark shuffle 相关细节整理
1.Shuffle Write 和Shuffle Read具体发生在哪里 2.哪里用到了Partitioner 3.何为mapSideCombine 4.何时进行排序 之前已经看过spark shuf ...
随机推荐
- gd库不支持jpeg的解决方法
杜工就不在这里啰嗦怎么遇到这个问题的了,如果你确实安装了的gd库,却发现无法支持jpeg格式的图片,可从下面找到答案. 原因是在编译gd库前,配置时未声明jpeg库路径.解决方法如下: 32位系统: ...
- Software Industry Revolution----POJ3898----DP
题目地址:http://poj.org/problem?id=3898 题目意思: 给你一个模式串,再给你一个原串,要你去匹配 模式串里面的?可对应任意一个字符 *号可对应0个或多个字符 其中a=1, ...
- ZOJ 3529 A Game Between Alice and Bob 博弈好题
A Game Between Alice and Bob Time Limit: 5 Seconds Memory Limit: 262144 KB Alice and Bob play t ...
- 开发一个微信小程序教程
一.注册小程序账号 1.进入微信公众平台(https://mp.weixin.qq.com/),注册小程序账号,根据提示填写对应的信息即可. 2.注册成功后进入首页,在 小程序发布流程->小程序 ...
- js处理层级数据结构的一些总结
开发者对复杂的数据结构的处理能力也是体现开发者水平的一个度量吧...最近发现自己对一些嵌套数据结构.层级数据结构的处理能力不大足...经常被这些把自己绕晕...严重影响开发效率...就稍微低总结了一下 ...
- Jmeter对基于websocket协议的压力测试
WebSocket protocol 是HTML5一种新的协议.它实现了浏览器与服务器全双工通信(full-duplex). 浏览器和服务器只需要要做一个握手的动作,然后,浏览器和服务器之间就 ...
- HttpContext.Current.Request.ServerVariables
Request.ServerVariables("Url") 返回服务器地址Value 0: /WebSite1/Default.aspx Request.ServerVariab ...
- Influxdb原理详解
本文属于<InfluxDB系列教程>文章系列,该系列共包括以下 15 部分: InfluxDB学习之InfluxDB的安装和简介 InfluxDB学习之InfluxDB的基本概念 Infl ...
- 【51Nod】1005 大数加法
给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 10000 需注意:A B有可能为负数) Output 输出A + B Input示例 ...
- KB2533623 下载
服务器上要部署.NET Core 的环境, 先要在服务器上安装Core SDK.直达连接 下载安装一切顺利: 下面开始检验是否正确安装了↓ 运行→cmd→dotnet 结果报错↓ Failed to ...