CF1187F Expected Square Beauty

Expected Square Beauty

有一个长度为 n 的数列,第 i 个数的取值范围为 \([l_i,r_i]\) ,定义一个数列的价值为这个数列极长连续相同段的个数,求一个数列价值的平方期望,对 \(10^9+7\) 取模 。

n≤200000 。

题解

https://codeforces.com/blog/entry/68111

As usual with tasks on an expected value, let‘s denote \(I_i(x)\) as indicator function: \(I_i(x) = 1\) if \(x_i \neq x_{i - 1}\) and \(0\) otherwise; \(I_1(x) = 1\). Then we can note that \(B(x) = \sum\limits_{i = 1}^{n}{I_i(x)}\). Now we can make some transformations:

\[
E(B^2(x)) = E((\sum\limits_{i = 1}^{n}{I_i(x)})^2) = E(\sum\limits_{i = 1}^{n}{ \sum\limits_{j = 1}^{n}{I_i(x) I_j(x)} }) = \sum\limits_{i = 1}^{n}{ \sum\limits_{j = 1}^{n}{E(I_i(x) I_j(x))} }
\]

Now we‘d like to make some casework:

  • if |i - j| > 1 (i and j aren‘t consecutive) then \(I_i(x)\) and \(I_j(x)\) are independent, that‘s why \(E(I_i(x) I_j(x)) = E(I_i(x)) E(I_j(x))\);
  • if i = j then \(E(I_i(x) I_i(x)) = E(I_i(x))\);
  • |i - j| = 1 need further investigation.

For the simplicity let‘s transform segment \([l_i, r_i]\) to \([l_i, r_i)\) by increasing \(r_i = r_i + 1\).

Let‘s denote \(q_i\) as the probability that \(x_{i-1} = x_i\):

\[
q_i = \max(0, \frac{\min(r_{i - 1}, r_i) - \max(l_{i - 1}, l_i)}{(r_{i - 1} - l_{i - 1})(r_i - l_i)})
\]

and \(q_1 = 0\). Let‘s denote \(p_i = 1 - q_i\). In result, \(E(I_i(x)) = p_i\).

The final observation is the following: \(E(I_i(x) I_{i + 1}(x))\) is equal to the probability that \(x_{i - 1} \neq x_i\) and \(x_i \neq x_{i + 1}\) and can be calculated by inclusion-exclusion principle:

\[
E(I_i(x) I_{i + 1}(x)) = 1 - q_{i} - q_{i + 1} + P(x_{i-1} = x_i\ \&\ x_i = x_{i + 1})
\]

, where

\[
P(x_{i-1} = x_i\ \&\ x_i = x_{i + 1}) = \max(0, \frac{\min(r_{i-1}, r_i, r_{i+1}) - \max(l_{i-1}, l_i, l_{i+1})}{(r_{i-1} - l_{i-1})(r_i - l_i)(r_{i+1} - l_{i+1})}).
\]

In result,

\[
E(B^2(x)) = \sum\limits_{i = 1}^{n}{( p_i + p_i\sum\limits_{|j - i| > 1}{p_j} + E(I_{i-1}(x) I_i(x)) + E(I_i(x) I_{i+1}(x)) )}
\]

and can be calculated in \(O(n \log{MOD})\) time.

CO int N=2e5+10;
int L[N],R[N];
int Q[N],E[N];

int calc(int i,int j,int k){
    int prob=0;
    if(i>=1){
        int l=max(L[i],max(L[j],L[k])),r=min(R[i],min(R[j],R[k]));
        if(l<r) prob=mul(r-l,fpow(mul(R[i]-L[i],
                     mul(R[j]-L[j],R[k]-L[k])),mod-2));
    }
    return add(1,add(mod-Q[j],add(mod-Q[k],prob)));
}

int main(){
    int n=read<int>();
    for(int i=1;i<=n;++i) read(L[i]);
    for(int i=1;i<=n;++i) read(R[i]),++R[i];
    int sum=0;
    for(int i=1;i<=n;++i){
        int l=max(L[i],L[i-1]),r=min(R[i],R[i-1]);
        if(l<r) Q[i]=mul(r-l,fpow(mul(R[i-1]-L[i-1],R[i]-L[i]),mod-2));
        E[i]=add(1,mod-Q[i]);
        sum=add(sum,E[i]);
    }
    int ans=0;
    for(int i=1;i<=n;++i){
        int res=sum;
        for(int j=max(i-1,1);j<=min(i+1,n);++j) res=add(res,mod-E[j]);
        ans=add(ans,mul(E[i],res));
        ans=add(ans,E[i]);
        if(i>1) ans=add(ans,calc(i-2,i-1,i));
        if(i<n) ans=add(ans,calc(i-1,i,i+1));
    }
    printf("%d\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/autoint/p/12109871.html

时间: 2024-08-01 12:48:02

CF1187F Expected Square Beauty的相关文章

codeforces 1187F. Expected Square Beauty

求$E({B(x)}^2)$,考虑$B(x)$为每一位与前一位不同的期望次数 令$A(x)$表示第$x$位与第$x-1$位不同的概率,特别地,$A(1)=1$ $$E({B(x)}^2)=E({(\sum_{i=1}^n A(i))}^2)$$ 把式子展开得, $$E({B(x)}^2)=\sum_{i=1}^n \sum_{j=1}^n E(A(i)\times A(j))$$ 显然如果$|i-j|>1$,$A(i)$与$A(j)$是独立的,$E(A(i)\times A(j))=E(A(i)

PRML 2: Regression Models

1. Linear Regression Model The Least Square Method is derived from maximum likelihood under the assumption of a Gaussian noise distribution, and hence could give rise to the problem of over-fitting, which is a general property of MLE. The Regularized

1.5 Scipy:高级科学计算

医药统计项目可联系 QQ:231469242 http://www.kancloud.cn/wizardforcel/scipy-lecture-notes/129867 作者:Adrien Chauve, Andre Espaze, Emmanuelle Gouillart, Ga?l Varoquaux, Ralf Gommers Scipy scipy包包含许多专注于科学计算中的常见问题的工具箱.它的子模块对应于不同的应用,比如插值.积分.优化.图像处理.统计和特殊功能等. scipy可以

python之scipy模块

一  简单介绍 SciPy是基于NumPy开发的高级模块,它提供了许多数学算法和函数的实现,用于解决科学计算中的一些标准问题.例如数值积分和微分方程求解,扩展的矩阵计算,最优化,概率分布和统计函数,甚至包括信号处理等. 作为标准科学计算程序库,SciPy类似于Matlab的工具箱,它是Python科学计算程序的核心包,它用于有效地计算NumPy矩阵,与NumPy矩阵协同工作. SciPy库由一些特定功能的子模块构成,如下表所示: 模块 功能 cluster 矢量量化 / K-均值 constan

good article————K’th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)

这是本人在研究leetcode中Median of Two Sorted Arrays一题目的时候看到一篇文章,觉得非常好,其中对快速排序重新实现. 文章来源于http://www.geeksforgeeks.org/这个网站. We recommend to read following post as a prerequisite of this post. K'th Smallest/Largest Element in Unsorted Array | Set 1 Given an ar

[leetcode] 367. Valid Perfect Square

Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False 使用二分查找寻找input

webpack 打包压缩 ES6文件报错UglifyJs + Unexpected token punc &#171;(&#187;, expected punc &#171;:&#187;

webpack打包压缩 ES6 js..vue报错: ERROR in js/test.js from UglifyJs Unexpected token punc ?(?, expected punc ?:? [js/test.js:1374,5] 解决方案: 配置babel,把配置放到文件[.babelrc]中 { "presets": ["es2015"] }

Project Euler 92:Square digit chains C++

A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before. For example, 44 → 32 → 13 → 10 → 1 → 1 85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89 Therefore any chain that

LeetCode Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. 思路分析:这题考察DP.缓存中间结果降低反复计算.DP方程为 if(matrix[i][j