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 ...
随机推荐
- 解决TXT乱码问题
初装Ubuntu,打开windows保存的txt文件很可能会遇到各种乱码问题. 下面是wiki ubuntu里的解决办法: Gedit中文乱码 缺省配置下,用 Ubuntu 的文本编辑器(gedit) ...
- [置顶] 使用sping AOP 操作日志管理
记录后台操作人员的登陆.退出.进入了哪个界面.增加.删除.修改等操作 在数据库中建立一张SYSLOG表,使用Sping 的AOP实现日志管理,在Sping.xml中配置 <!-- Spring ...
- VS2012下基于Glut 矩阵变换示例程序:
也可以使用我们自己的矩阵运算来实现OpenGL下的glTranslatef相应的旋转变换.需要注意的是OpenGL下的矩阵是列优先存储的. 示例通过矩阵运算使得圆柱或者甜圈自动绕Y轴旋转,可以单击鼠标 ...
- 【汇编】字符串处理指令 stosb、lodsb、movsw、scasb、rep
一.字符串处理指令 (1) lodsb.lodsw:把DS:SI指向的存储单元中的数据装入AL或AX,然后根据DF标志增减SI (2) stosb.stosw:把AL或AX中的数据装入ES:DI指向的 ...
- Maven之pom.xml 配置详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- FragmentTransation中的remove和detach有什么区别?
remove(): 从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈(回退栈后面会详细说),这个Fragment实例将会被销毁; detach(): 会将vi ...
- WPF 使用WinForm Chart控件
第一步: 页面 首先引用命名空间 xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFor ...
- Kali命令集
转载自:http://www.shiyanbar.com/questions/980 系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显 ...
- 关于onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
API 21为Activity增加了一个新的属性,只要将其设置成persistAcrossReboots,activity就有了持久化的能力,另外需要配合一个新的bundle才行,那就是Persist ...
- solr最佳实践
管理页面 页面地址:http://{ip}:{port}/solr/#/ 管理页面的data-import页可以手动重建索引,configuration指定了数据源,重建索引也可以通过http请求触发 ...