You are given an array a consisting of n positive integers. You pick two integer numbers l and r from 1 to n, inclusive (numbers are picked randomly, equiprobably and independently). If l > r, then you swap values of l and r. You have to calculate the expected value of the number of unique elements in segment of the array from index l to index r, inclusive (1-indexed).

Input

The first line contains one integer number n (1 ≤ n ≤ 106). The second line containsn integer numbers a1a2, ... an (1 ≤ ai ≤ 106) — elements of the array.

Output

Print one number — the expected number of unique elements in chosen segment.

Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 4 — formally, the answer is correct if , where xis jury's answer, and y is your answer.

Example

Input
2
1 2
Output
1.500000
Input
2
2 2
Output
1.000000

题意:给你一个序列a,随机生成l,r有可能l>r,则看做r,l处理。将权值定义为l-r中不同数字的个数,求期望。

题解:第一道期望题!首先肯定是想暴力,枚举每一种l,r,扫l-r中不同的数的个数,复杂度(n^3),考虑优化,对于每个值k,他能对前一个值为k的位置到如今的所有l和之后的所有r做贡献,贡献为(i-pre[k])*(n-i+1),因为l,r可互换,所以有两种可能性,所以贡献为(i-pre[k])*(n-i+1)。
因为l=r的情况只算一种,但之前算了两遍,所以应该减去。 代码如下:
#include<cstdio>
#include<algorithm>
using namespace std; long long pre[],n,i,j,ans; int main()
{
scanf("%lld",&n);
ans-=n;
for(i=;i<=n;i++)
{
long long x;
scanf("%lld",&x);
ans+=(i-pre[x])*(n-i+)*;
pre[x]=i;
}
double hehe=(double)ans/(n*n);
printf("%.6lf",hehe);
}

每天刷题,身体棒棒!

CodeForces - 846F Random Query(期望)的更多相关文章

  1. Codeforces 846F - Random Query

    原题链接:http://codeforces.com/contest/846/problem/F 题意:给一个数列,任意取区间[l, r],问区间内不同数字的个数的期望是多少. 思路: 对于第i个数a ...

  2. codeforces 478B Random Teams

    codeforces   478B  Random Teams  解题报告 题目链接:cm.hust.edu.cn/vjudge/contest/view.action?cid=88890#probl ...

  3. Random Query CodeForces - 846F

    题目 翻译: 给出一个n个数字的数列a[1],...,a[n],f(l,r)表示使a[l],a[l+1],...,a[r]组成的新序列中的重复元素只保留一个后,剩下元素的数量(如果l>r,则在计 ...

  4. CodeForces 912d fishes(优先队列+期望)

    While Grisha was celebrating New Year with Ded Moroz, Misha gifted Sasha a small rectangular pond of ...

  5. codeforces 478B Random Teams 解题报告

    题目链接:http://codeforces.com/problemset/problem/478/B 题目意思:有 n 个人,需要将这班人分成 m 个 组,每个组至少含有一个人,同一个组里的人两两可 ...

  6. Codeforces.24D.Broken robot(期望DP 高斯消元)

    题目链接 可能这儿的会更易懂一些(表示不想再多写了). 令\(f[i][j]\)表示从\((i,j)\)到达最后一行的期望步数.那么有\(f[n][j]=0\). 若\(m=1\),答案是\(2(n- ...

  7. codeforces 24d Broken robot 期望+高斯消元

    题目传送门 题意:在n*m的网格上,有一个机器人从(x,y)出发,每次等概率的向右.向左.向下走一步或者留在原地,在最左边时不能向右走,最右边时不能像左走.问走到最后一行的期望. 思路:显然倒着算期望 ...

  8. CodeForces 1067E Random Forest Rank

    题意 给定一棵 \(n\) 个节点的树,每条边有 \(\frac{1}{2}\) 的概率出现,这样会得出一个森林,求这个森林的邻接矩阵 \(A\) 的秩 \(\operatorname{rank} A ...

  9. Codeforces 1067E - Random Forest Rank(找性质+树形 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 一道不知道能不能算上自己 AC 的 D1E(?) 挺有意思的结论题,结论倒是自己猜出来了,可根本不会证( 开始搬运题解 ing: 碰到这样 ...

随机推荐

  1. Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SpringStruts]]

    今天启动Tomcat时候遇到了这个问题 Failed to start component [StandardEngine[Catalina].StandardHost[localhost].Stan ...

  2. java实现excel和数据的交互

    1. 环境要求 本文环境为: 数据库为oracle,jdk为jdk7,依赖jar包为ojdbc6-11.2.0.4.0.jar+poi-3.14.jar 2.POI 使用 1. 建立工作空间 2. 获 ...

  3. String类的构造方法(2)

    写了常见的几个而已. 1:new 一个String类的时候系统会自动传一个空构造 public String(); 注意: 当对象初始化是 null时 和 对象是 "" 时,两者是 ...

  4. 用postal.js在AngularJS中实现订阅发布消息

    点击查看AngularJS系列目录 用postal.js在AngularJS中实现event bus 用postal.js在AngularJS中实现event bus 理想状态下,在一个Angular ...

  5. 2.bootstrap-全局css

    1.Bootstrap 网格系统 Bootstrap 提供了一套响应式.移动设备优先的流式网格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列. 1.基本的网格结构 下面是 ...

  6. Linux入门之常用命令(4)vi编辑器

    vi分为三种模式 一般模式:删除字符.删除整行.复制粘贴等操作 编辑模式:i o a r进入 输入字符  Esc退出 命令行模式::或/ 将光标移动到最末行 搜寻数据 读取或替换 退出vi 显示行号 ...

  7. Linux crontab任务调度

    一.crontab说明 Linux crontab任务调度是在规定的时间频率内去执行相应的任务. 二.crontab文件详情 1.crontab文件在Linux中的/etc/crontab 2.查看c ...

  8. The Twin Towers zoj2059 DP

    The Twin Towers Time Limit: 2 Seconds      Memory Limit: 65536 KB Twin towers we see you standing ta ...

  9. 1289 大鱼吃小鱼 1305 Pairwise Sum and Divide 1344 走格子 1347 旋转字符串 1381 硬币游戏

    1289 大鱼吃小鱼 有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右.游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼.从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右 ...

  10. ESLint可共享配置发布,团队自定义ESLint规则新鲜出炉

    ESLint于2013年6月份推出,至今4个年头,最新版本v4.8.0.它是目前主流的用于Javascript和JSX代码规范检查的利器,很多大公司比如Airbnb和Google均有一套自己的Java ...