Codeforces 846F - Random Query 【期望】


F. Random Query

time limit per test  2seconds

memory limit per test      256megabytes


You are given an array a consisting of n positive integers. You pick twointeger numbers l and r from 1 to n, inclusive (numbers are picked randomly, equiprobably andindependently). If l > r, then you swap values of l and r. You have to calculate the expectedvalue 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 contains n integer numbers a1, a2, ... an(1 ≤ ai ≤ 106) — elements of the array.

Output

Print one number — the expected number of unique elements inchosen segment.

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

Examples

Input

2
1 2

Output

1.500000

Input

2
2 2

Output

1.000000

 

【题意】


给出n个数,需要计算从1~n中选出两个数(可以相同)x,y,令l=min(x,y),r=max(x,y),区间[l,r]内不同数个数的期望值。


【思路】


显然期望值为所有情况(共n*n种)的不同数个数和,再除以情况数。


那么我们要做的便是在限制时间内算出所有情况的不同数个数和。


由于x,y的大小不确定,但是其实无论谁大谁小对结果的影响不大,所以我们先固定x<=y。


假设我们现在在考虑第i个数,且已经算出了前(i-1)个数所有区间的不同数个数。


  1. 如果它他前面没有与它相同的数,那么它的贡献为使每个区间都多一个数,即[1,i],[2,i],[3,i],[4,i]....[i,i],

  2. 如果它前面有与之相同的数,前一次出现的位置为pos,那么对于区间[1,i],[2,i]...[pos,i]来说不同数的个数并没有改变,当然剩下的区间由于之间不包括这个数,每个区间不同数的个数会加一。


由上面的分析,我们可以推出:假设前(i-1)个的所有区间(固定x<=y)不同数个数和为sum[i-1],那么前i个的所有区间(固定x<=y)不同数个数和为sum[i-1]+i-pre[i],其中pre[i]为第i个数前一次出现的位置(没有则为0)。


然后考虑x,y大小的任意性,每个数的贡献即为sum[i]*2-1,减一的原因为当x,y相等时只有一种情况。


于是扫一遍累加结果再除以情况数即可。


#include <cstdio>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)


typedef long long ll;
const int maxn = 1000005;
const ll mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;

int a[maxn],pre[maxn];

int main()
{
    int n,x;
    while(~scanf("%d",&n))
    {
        mst(pre,0);
        ll ans=0;
        ll temp=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            temp+=i-pre[x];
            pre[x]=i;
            ans+=(ll)temp*2-1;
        }
        printf("%.6f\n",ans*1.0/n/n);
    }
    return 0;
}








  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值