Educational Codeforces Round 28 F. Random Query

Posted 早知如此绊人心,何如当初莫相识。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Educational Codeforces Round 28 F. Random Query相关的知识,希望对你有一定的参考价值。

题意:一个数列,随机选l,r,f(l,r)为l,r区间内数的种数,问f(l,r)的期望

思路:sum(每个数算出他的贡献)/(n*n),我们这只考虑l<=r ,对于当前这数字他能贡献后面的所有区间,但是对于前面的话,他只共贡献到前一个相同的数后面

           比如  1  2  3  4  2  5  6

           对于第一个2  他贡献于  (1,2) (1,3)(1,4)(1,5)(1,6)(1,7)

              (2,2) (2,3)(2,4)(2,5)(2,6)(2,7)

   对于第2个2  贡献于     (3,5)(3,6)(3,7)     (4,5)(4,6)(4,7)     (5,5)(5,6)(5,7)

  注意:爆int

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 int a[1000005];
 5 
 6 int main(){
 7     int n;
 8     ll ans=0;
 9     cin>>n;
10     for(int i=1;i<=n;i++){
11         int x;
12         scanf("%d",&x);
13         ans+=1LL*(i-a[x])*(n-i+1);
14        // cout<<(i-a[x])<<" "<<(n-i+1)<<" "<<ans<<endl;
15         a[x]=i;
16     }
17     ans=ans*2-n;
18     //cout<<ans<<endl;
19     printf("%.5f\n",ans*1.0/(1LL*n*n*1.0));
20 }

 

   

以上是关于Educational Codeforces Round 28 F. Random Query的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 7 A

Educational Codeforces Round 7

Educational Codeforces Round 90

Educational Codeforces Round 33

Codeforces Educational Codeforces Round 54 题解

Educational Codeforces Round 27