Random Query CodeForces - 846F
翻译:
给出一个n个数字的数列a[1],...,a[n],f(l,r)表示使a[l],a[l+1],...,a[r]组成的新序列中的重复元素只保留一个后,剩下元素的数量(如果l>r,则在计算前先交换l和r)。从1-n中分别选出两个数字l和r(两个数字选时各自独立,每个数字选出1-n的概率相等),求f(l,r)的数学期望。
就是$\dfrac {\sum _{l\leq n}^{l=1}\sum _{r\leq n}^{r=1}f\left( l,r\right) } {n^{2}}$
然后,我参照这道题的做法写了一个O(log(n)*n^2)
#include<cstdio>
typedef long long LL;
LL c[];
LL n,ans;
LL a[];
LL next1[];
LL boo[];
LL lowbit(LL x)
{
return x&-x;
}
void add(LL num,LL x)
{
while(num<=n)
{
c[num]+=x;
num+=lowbit(num);
}
}
LL sum1(LL x)
{
LL ans=;
while(x>)
{
ans+=c[x];
x-=lowbit(x);
}
return ans;
}
LL sum(LL l,LL r)
{
return sum1(r)-sum1(l-);
}
int main()
{
LL i,j;
scanf("%lld",&n);
for(i=;i<=n;i++)
scanf("%lld",&a[i]);
for(i=n;i>=;i--)
next1[i]=boo[a[i]],boo[a[i]]=i;
//boo[i]记录数字i第一次出现位置,next1[i]记录第i个数字下一次出现位置
for(i=;i<=n;i++)
if(boo[a[i]]==i)
add(i,);
for(i=;i<=n;i++)
{
for(j=i+;j<=n;j++)
ans+=sum(i,j);
if(next1[i]!=)
add(next1[i],);
}
ans*=;
ans+=n;
printf("%lf",(double)ans/(double)n/(double)n);
return ;
}
然后,就tle了。。。
经过大佬的指点后,我打开了新世界的大门找到了新的方法
例如:1 2 2 2 3
第1个2对1-2,1-3,1-4,1-5,2-2,2-3,2-4,2-5的不相同值的数量有贡献
第2个2对3-3,3-4,3-5的不相同值的数量有贡献
第3个2对4-4,4-5的不相同的值的数量有贡献
由于每一组a-b,b-a(a<b)只计算了其中的一个(a-b),所以要乘2。
由于乘2后所有a-a计算2遍,所以要减去n。
#include<cstdio>
typedef long long LL;
//bool exist[1001000];
int last[];
LL ans,n;
int main()
{
LL i,t;
scanf("%lld",&n);
for(i=;i<=n;i++)
{
scanf("%lld",&t);
ans+=(i-last[t])*(n-i+);
last[t]=i;
//(last[t]+1)-i,..,(last[t]+1)-n,.....,i-i,..,i-n
//last[t]+1:n-i+1;...i:n-i+1
// if(exist[t])
// ans+=n-i+1;
// else
// {
// exist[t]=true;
// //ans+=i*(n-i+1);
// //n+(n-1)+...+(n-i+1)
// ans+=(2*n-i+1)*i/2;
// }
//错误,无法处理如2 2 4 2的数据
}
printf("%lf",(double)(ans*-n)/(double)n/(double)n);
return ;
}
Random Query CodeForces - 846F的更多相关文章
- Codeforces 846F - Random Query
原题链接:http://codeforces.com/contest/846/problem/F 题意:给一个数列,任意取区间[l, r],问区间内不同数字的个数的期望是多少. 思路: 对于第i个数a ...
- CodeForces - 846F Random Query(期望)
You are given an array a consisting of n positive integers. You pick two integer numbers l and r fro ...
- Subtree Minimum Query CodeForces - 893F (线段树合并+线段树动态开点)
题目链接:https://cn.vjudge.net/problem/CodeForces-893F 题目大意:给你n个点,每一个点有权值,然后这n个点会构成一棵树,边权为1.然后有q次询问,每一次询 ...
- B. Random Teams(Codeforces Round 273)
B. Random Teams time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- 【Educational Codeforces Round28】
咸鱼选手发现自己很久不做cf了,晚节不保. A.Curriculum Vitae 枚举一下间断点的位置. #include<bits/stdc++.h> using namespace s ...
- python爬取微信公众号
爬取策略 1.需要安装python selenium模块包,通过selenium中的webdriver驱动浏览器获取Cookie的方法.来达到登录的效果 pip3 install selenium c ...
- iOS SwiftMonkey 随机暴力测试
参考源文章 https://github.com/zalando/SwiftMonkey https://kemchenj.github.io/2017/03/16/2017-03-16/ 简介 这个 ...
- 如何使用jMeter对某个OData服务进行高并发性能测试
For project reason I have to measure the performance of OData service being accessed parallelly. And ...
- MySQL将语句写入到binlog二进制日志中
由于二进制日志是公共资源,所有线程都要写二进制日志,所以一定要避免两个线程同时更新二进制日志.因此,在事件组写二进制日志时,二进制日志将获得一个互斥锁LOCK_log,然后在事件组写完后释放,由于服务 ...
随机推荐
- notepad++的f90配置文件
notepad++仅支持f77格式的,所以f90格式需要重新定义配置文件 传送门: http://ehc.ac/p/notepad-plus/discussion/331753/thread/8f72 ...
- python selenium实现百度搜索
1.环境 python2.7+selenium+phantomjs+linux 2.代码 #-*-coding:utf-8 -*- from selenium import webdriver fro ...
- POJ1797 Heavy Transportation —— 最短路变形
题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K T ...
- android项目 res/ 目录内支持的资源目录详解
表 1. 项目 res/ 目录内支持的资源目录 目录 资源类型 animator/ 用于定义属性动画的 XML 文件. anim/ 定义渐变动画的 XML 文件.(属性动画也可以保存在此目录中,但是为 ...
- android按压背景
android:background="?android:actionBarItemBackground"
- [Selenium] 测试机器上安装了多个Firefox,如何指定运行哪一个?
可通过FirefoxBinary 来指定运行某个路径下的Firefox, 示例代码如下: public class testFirefoxBinary{ public static void main ...
- 关于GitHub的DNS基础设施,你了解吗?
在 GitHub,我们最近从头改进了 DNS.这包括了我们如何与外部 DNS 提供商交互以及我们如何在内部向我们的主机提供记录.为此,我们必须设计和构建一个新的 DNS 基础设施,它可以随着 GitH ...
- mac下载模块时报错OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/chardet'
原文地址:https://www.cnblogs.com/liangyan-1989/p/8143129.html 安装完pip后,使用pip install selenium报以下错 OSError ...
- 基于active状态类名的web交互开发
demo: 默认是这样的: 当我们点击更多按钮后,会以CSS3动画的方式展开剩余所有文字信息,如下动图: 下面我们看下关键的CSS代码,这个.active是如何自身无样式的: .box { max-h ...
- 879C
贪心 题目看错了...还以为是从操作序列中选5个...然后半个小时没了... 我们把每位分别用0和1带入,看看返回值是什么,然后分类讨论.千万不用特判!!!之前忘了删了就fst... #include ...