2018牛客网暑期ACM多校训练营(第五场) F - take - [数学期望][树状数组]
题目链接:https://www.nowcoder.com/acm/contest/143/F
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
Kanade has n boxes , the i-th box has p[i] probability to have an diamond of d[i] size.
At the beginning , Kanade has a diamond of 0 size. She will open the boxes from 1-st to n-th. When she open a box,if there is a diamond in it and it's bigger than the diamond of her , she will replace it with her diamond.
Now you need to calculate the expect number of replacements.
You only need to output the answer module 998244353.
Notice: If x%998244353=y*d %998244353 ,then we denote that x/y%998244353 =d%998244353
输入描述:
The first line has one integer n. Then there are n lines. each line has two integers p[i]*100 and d[i].
输出描述:
Output the answer module 998244353
输入
3
50 1
50 2
50 3
输出
499122178
备注:
1<= n <= 100000 1<=p[i]*100 <=100 1<=d[i]<=10^9
题意:
有n个盒子,每个盒子里有p[i]的概率有一颗d[i]大小的钻石,Kanade现在手上有一颗0大小的钻石,他遇到比手上大的钻石就会进行交换,
现在Kanade从1~n打开盒子,计算交换次数的期望。
Notice:
If x%998244353=y*d %998244353 ,then we denote that x/y%998244353 =d%998244353
这句话提示我们如何用整数表示小数,我们定 (p/100)%998244353 = d%998244353,这个d满足 (100*d)%998244353 = p%998244353,
这个整数d,就相当于p/100。
题解:
对于第 i 个盒子,选取这颗钻石进行交换的概率是:前面 1 ~ i-1 颗钻石中比这颗大的那些,都没有出现的概率,乘上当前这颗钻石出现的概率,
即 $p\left[ i \right]\prod\limits_{j < i,d\left[ i \right] < d\left[ j \right]} {\left( {1 - p\left[ j \right]} \right)}$,
而交换次数的期望,就等于求和:“每个盒子交换的概率乘以交换1次(数值上就等于概率)”。
但是不可能 $O\left( {n^2 } \right)$ 过,所以考虑前缀优化,我们可以用树状数组维护原序列的前缀积,
再把盒子按 $d\left[ i \right]$ 降序排序,然后进行枚举,
此时,对于第 i 个盒子,比体积它大的都已经计算过了,都存在树状数组里了,就可以直接查询。
(参考:https://www.nowcoder.com/discuss/89992?type=101&order=0&pos=1&page=0)
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=+;
const ll MOD=; int n;
struct Box{
ll p,d;
int id;
}box[maxn];
bool cmp(Box a,Box b)
{
if(a.d==b.d) return a.id<b.id;
return a.d>b.d;
} struct _BIT //单点增加,区间查询
{
int n;
ll C[maxn];
int lowbit(int x){return x&(-x);}
void init(int n)
{
this->n=n;
for(int i=;i<=n;i++) C[i]=;
}
void add(int pos,ll val) //在pos点乘上val
{
while(pos<=n)
{
C[pos]=C[pos]*val%MOD;
pos+=lowbit(pos);
}
}
ll ask(int pos) //查询1~pos点的积
{
ll ret=;
while(pos>)
{
ret=ret*C[pos]%MOD;
pos-=lowbit(pos);
}
return ret;
}
}BIT; ll pow(ll a,ll b) //快速幂
{
ll r=,base=a%MOD;
while(b){
if(b&) r*=base , r%=MOD;
base*=base;
base%=MOD;
b>>=;
}
return r;
}
ll inv(ll a){return pow(a,MOD-);} //求逆元 int main()
{
cin>>n;
BIT.init(n);
for(int i=;i<=n;i++)
{
cin>>box[i].p>>box[i].d;
box[i].id=i;
}
sort(box+,box+n+,cmp); ll inv100=inv();
ll ans=;
for(int i=;i<=n;i++)
{
ans+=(box[i].p*inv100)%MOD * BIT.ask(box[i].id-)%MOD;
ans%=MOD;
BIT.add(box[i].id,(-box[i].p)*inv100%MOD);
}
cout<<ans<<endl;
}
2018牛客网暑期ACM多校训练营(第五场) F - take - [数学期望][树状数组]的更多相关文章
- 2018牛客网暑期ACM多校训练营(第二场)J Farm(树状数组)
题意 n*m的农场有若干种不同种类作物,如果作物接受了不同种类的肥料就会枯萎.现在进行t次施肥,每次对一个矩形区域施某种类的肥料.问最后枯萎的作物是多少. 分析 作者:xseventh链接:https ...
- 牛客网暑期ACM多校训练营(第二场)J farm (二维树状数组)
题目链接: https://www.nowcoder.com/acm/contest/140/J 思路: 都写在代码注释里了,非常好懂.. for_each函数可以去看一下,遍历起vector数组比较 ...
- 2018牛客网暑期ACM多校训练营(第二场):discount(基环树DP)
题意:有N个不同的商品,每个商品原价是Pi元,如果选择打折,可以减少Di元. 现在加一种规则,每个商品有一个友好商品Fai,如果i用原价买,则可以免费买Fai. 现在问买到所有物品的最小价格. 思路 ...
- 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)
2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...
- 2018牛客网暑期ACM多校训练营(第一场)D图同构,J
链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...
- 2018 牛客网暑期ACM多校训练营(第一场) E Removal (DP)
Removal 链接:https://ac.nowcoder.com/acm/contest/139/E来源:牛客网 题目描述 Bobo has a sequence of integers s1, ...
- 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)
题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...
- 2018牛客网暑期ACM多校训练营(第二场) J - farm - [随机数哈希+二维树状数组]
题目链接:https://www.nowcoder.com/acm/contest/140/J 时间限制:C/C++ 4秒,其他语言8秒 空间限制:C/C++ 262144K,其他语言524288K ...
- 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)
链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...
- 2018牛客网暑期ACM多校训练营(第二场) A - run - [DP]
题目链接:https://www.nowcoder.com/acm/contest/140/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K ...
随机推荐
- Mac下配置svn服务器
Mac OS X 系统已经内置了svn支持,所以需要做的就只是配置,可以用svnadmin –vsersion(svnserve –version)查看.希望能对 您配置 SVN.进行开发版本控制有所 ...
- 解决win10休眠后无法唤醒
在控制面板-电源选项-编辑计划设置-高级电源设置中把"睡眠"的选项中休眠调整为从不,"电源按键和盖子"选项中也都设为睡眠,这样使得无论你是使用电池还是电源,系统 ...
- 【NGINX】Windows配置
缺省安装nginx之后的配置 检查80端口占用 启动缺省配置之后的nginx 配置多端口站点 注册Server,server_name是配置的域名,proxy_pass是上图配置的代理地址 注意: 1 ...
- The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make s
我出现这个问题是引用资源文件问题 helper.getView(R.id.in_pic).setBackgroundResource(item.getResourceId()); //错的helper ...
- Linux下的XAMPP基本配置技巧(设置虚拟主机、添加FTP账户等)
xampp安装好之后就只有一个默认站点及一个默认nobody的ftp账户,这显然不符合我们平时的需求了,那么下面就来讲一下如何设置并管理多个虚拟主机及ftp账户了,至于xampp的安装不在此讨论范围, ...
- open-falcon之alarm、sender、links说明.md
alarm 功能 处理judge 产生的告警event 区分告警优先级,优先处理级别比较高的告警 为用户提供回调接口 生成告警msg 展示未恢复的告警 配置文件 { "debug" ...
- Linux 下如何安装 .bin 文件
拿到 .bin 文件,在文件所在目录下执行如下命令即可安装, ./your-file-name.bin 如果提示 “没有那个文件或目录” ,给它加上执行权限即可,执行如下命令, chmod +x ./ ...
- source.android.google && developer.android.google
https://source.android.google.cn/ https://developer.android.google.cn/ https://source.android.com/co ...
- spring boot 单元测试,如何使用profile
一.问题概述 spring boot项目.单元测试的时候,我发现,总是会使用application.properties的内容,而该文件里,一般是我的开发时候的配置. 比如上图中,dev是开发配置,p ...
- 编译安装Ruby 1.9.3 安装CentOS
1. 准备需要的安装的东西 yum -y install make gcc openssl-devel zlib-devel gcc gcc-c++ make autoconf readline-de ...