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 ...
随机推荐
- Class.forName不能加载abstract原因
今天看到单例模式时,突然想起,单例模式的情况是不让其他类来构造这个类本身,也就是不让new构造器,所以我们一般都会私有化这个构造器.我们知道abstract类是不能实例化的,我想利用abstract这 ...
- Nginx之旅系列 - Nginx日志功能 PK Linux内核printk
题记:Nginx之旅系列是用来记录Nginx从使用到源码学习的点点滴滴,分享学习Nginx的快乐 Nginx 首页: http://nginx.org/ Nginx日志功能 PK Linux内核pri ...
- Jenkins+PMD构建自动化静态代码检测
前言:软件缺陷是不可避免的,要尽量减少错误并提高软件质量,主要有两在类技术,即缺陷预防和缺陷检测 缺陷预防包括编写更好的设计规范.实施代码审核制度.运行代码静态分析工具.运行单元测试等 PMD是一种开 ...
- android 组件内部实现触摸事件,更改背景
当我们需要实现触摸事件的时候,通常就会想到OnTouchEvent这个方法,它能够获取触摸点的坐标,而组件同样也有这样的一个功能,使用这个功能,可以实现组件在父布局界面上移动,缩放,更改背景图片.这里 ...
- Xcode4.6 开发 metaio 增强现实 项目(二)--增强现实的实现
经过昨天的一些步骤,我们已经将我们的IOS应用的基本界面搭建好,下面我们将开始新的征程:增强现实技术的实现: 进入webView后,我们要选用我们这次需要用的nib文件,它是一个家居摆放应用的界面文件 ...
- 点击某一按钮新增click,并切换页面
应用场景:对于web端接收手机验证码的处理方法:1.如果有权限可以通过查询数据库来获得手机验证码,方便快捷.2.如果后台系统保存了手机验证码,可以去后台获取验证码,然后填写到前台页面,此方法有两种处理 ...
- 典型关联分析(CCA)原理总结
典型关联分析(Canonical Correlation Analysis,以下简称CCA)是最常用的挖掘数据关联关系的算法之一.比如我们拿到两组数据,第一组是人身高和体重的数据,第二组是对应的跑步能 ...
- 队列Queue FIFO先进先出 栈Stack FILO先进后出
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 抓包分析SIP消息
I. 实验环境 SIP服务器: Yate服务器, IP:port=10.15.3.34:5060 SIP客户端: 移植Sipdroid的Android网络电话软件, IP: 10.15.3.73, A ...
- js 增加 onclick 事件
obj.onclick = function(){check(this)} function check(obj){ alert(obj)l }