题意:

对于一个序列a,构造一个序列b,使得两个序列,对于任意的区间 [l, r] 的区间最靠近左端点的那个最大值的位置,并且序列 b 满足 0 < bi < 1。

给定一个序列 a ,求序列 b 中所有元素的和的期望。

Sample Input
3
3
1 2 3
3
1 2 1
5
1 2 3 2 1 Sample Output
250000002
500000004
125000001

题解:

若满足题意,则 a 和 b 的笛卡尔树同构。

因为 bi 在 0 到 1 之间,故 bi 的期望值为 1/2 ,所以 b 序列的和的期望值为 n/2。
对于笛卡尔树的每一棵子树,若用sz[i]表示以 i 为根节点的子树的大小,则满足其根节点是子树的最大值的概率为 1/sz[i] 。

所以整棵树满足条件的概率为1 / Πsz[i]。

故总期望值为两个的乘积 n / 2Πsz[i]。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
typedef long long LL;
const int maxn = ;
const int MOD = 1e9+; int a[maxn], lson[maxn], rson[maxn], fa[maxn];
LL dp[maxn], inv[maxn];
LL tmp; LL DP(int id)
{
if (id == ) return ;
if (dp[id]) return dp[id];
dp[id] = DP(lson[id]) + DP(rson[id]) + ;
return dp[id];
} void getinv()
{
inv[] = ;
for (int i = ; i < maxn; i++)
inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
} int main()
{
int t, n;
scanf("%d", &t);
getinv();
for (int ca = ; ca <= t; ca++)
{
scanf("%d", &n);
for (int i = ; i <= n; i++) scanf("%d", &a[i]); stack<int> st;
for (int i = ; i <= n; i++)
lson[i] = rson[i] = fa[i] = dp[i] = ; for (int i = ; i <= n; i++)
{
if (!st.empty() && a[st.top()] < a[i])
{
while (!st.empty() && a[st.top()] < a[i])
tmp = st.top(), st.pop();
lson[i] = tmp, fa[tmp] = i;
}
if (!st.empty()) rson[st.top()] = i, fa[i] = st.top();
st.push(i);
} for (int i = ; i <= n; i++) if (!fa[i]) DP(i); LL ans = n * inv[] % MOD;
for (int i = ; i <= n; i++) ans = ans * inv[dp[i]] % MOD; printf("%lld\n", ans);
}
}

2018 Multi-University Training Contest 1 H - RMQ Similar Sequence(HDU - 6305 笛卡尔树)的更多相关文章

  1. [模板] 笛卡尔树 && RMQ

    话说我noip之前为什么要学这种东西... 简介 笛卡尔树(Cartesian Tree) 是一种二叉树, 且同时具有以下两种性质: 父亲节点的值大于/小于子节点的值; 中序遍历的结果为原序列. 笛卡 ...

  2. HDU - 6305 RMQ Similar Sequence(笛卡尔树)

    http://acm.hdu.edu.cn/showproblem.php?pid=6305 题目 对于A,B两个序列,任意的l,r,如果RMQ(A,l,r)=RMQ(B,l,r),B序列里的数为[0 ...

  3. hdu 6305 RMQ Similar Sequence——概率方面的思路+笛卡尔树

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=6305 看题解,得知: 0~1内随机取实数,取到两个相同的数的概率是0,所以认为 b 序列是一个排列. 两个 ...

  4. 2016 Multi-University Training Contest 1 GCD RMQ+二分(预处理)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 题意:有N(N <= 100,000),之后有Q(Q <= 100,000)个区间查询[ ...

  5. 2018 Nowcoder Multi-University Training Contest 2

    目录 Contest Info Solutions A. run D. monrey G. transform H. travel I. car J. farm Contest Info Practi ...

  6. 2016 Al-Baath University Training Camp Contest-1 H

     Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...

  7. 2018 Nowcoder Multi-University Training Contest 1

    Practice Link J. Different Integers 题意: 给出\(n\)个数,每次询问\((l_i, r_i)\),表示\(a_1, \cdots, a_i, a_j, \cdo ...

  8. 2018 Nowcoder Multi-University Training Contest 5

    Practice Link A. gpa 题意: 有\(n\)门课程,每门课程的学分为\(s_i\),绩点为\(c_i\),要求最多删除\(k\)门课程,使得gpa最高. gpa计算方式如下: \[ ...

  9. 2018 Nowcoder Multi-University Training Contest 10

    Practice Link J. Rikka with Nickname 题意: 给出\(n\)个字符串,要求依次合并两个串\(s, t\),满足将\(t\)合并到\(s\)中变成\(r\),使得\( ...

随机推荐

  1. myBatis执行测试批量删除,出现测试类正常显示,但数据库数据没变

    一般在测试myBatis运行正常,但数据库数据不变时,有可能是SQL语句有问题,检查SQL语句没问题,但数据库依然没变,就说明myBatis方法执行后并未提交到数据库,可尝试在测试类添加   sess ...

  2. Fiddler 抓包工具总结(转)

    Fiddler 抓包工具总结   阅读目录 1. Fiddler 抓包简介 1). 字段说明 2). Statistics 请求的性能数据分析 3). Inspectors 查看数据内容 4). Au ...

  3. Vue-watch选项

    Vue ----watch 选项 用于 监听数据变化: <!DOCTYPE html> <html lang="en"> <head> < ...

  4. 解决The Network Adapter could not establish the connection

    解决1 主机与虚拟机ping不通 解决2 状态: 失败 -测试失败: IO 错误: The Network Adapter could not establish the connection 本次尝 ...

  5. Linux 下查找指令

    原文链接:http://www.cnblogs.com/sunleecn/archive/2011/11/01/2232210.html whereis <程序名称>查找软件的安装路径-b ...

  6. 使用kvm制作Eucalyptus镜像(Windows Server 2008r2为例)

    1.前言 Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) ...

  7. VMware Workstation Pro 11、12 密钥

    11:1F04Z-6D111-7Z029-AV0Q4-3AEH8 12:5A02H-AU243-TZJ49-GTC7K-3C61N

  8. HDU1075 字典树 + 字符串映射

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 ,字典树的字符串映射. 题意是给你每个火星文单词对应的英语,然后让你把一篇火星文文章给翻译成英语 ...

  9. HTML和CSS一般有哪些功能?(聊~平时常出现的那些知识)

    简单一点点 HTML行内标签有哪些? 一般行内的标签包含哪些? 如:a - 锚点, span - 常用内联或定义块级容器, i - 斜体, b - 粗体, strong - 粗体强调, var - 定 ...

  10. POJ 3187 Backward Digit Sums (递推,bruteforce)

    第1行j列的一个1加到最后1行满足杨辉三角,可以先推出组合数来 然后next_permutation直接暴. #include<cstdio> #include<iostream&g ...