HDU6305: RMQ Similar Sequence 题解

这篇博客介绍了如何解决HDU6305问题,即找到与给定序列RMQ相似的序列的期望权重。通过分析RMQ相似性的递归定义,博主提出了使用并查集的方法来高效地计算在0到1之间随机选取n个数并离散化后的排列,与原序列相似的排列的概率。最终,博主展示了如何计算期望权重,并给出了样例输入和输出。
摘要由CSDN通过智能技术生成

Description

Chiaki has a sequence A={a1,a2,…,an}. Let RMQ(A,l,r) be the minimum i (l≤i≤r) such that ai is the maximum value in al,al+1,…,ar.

Two sequences A and B are called \textit{RMQ Similar}, if they have the same length n and for every 1≤l≤r≤n, RMQ(A,l,r)=RMQ(B,l,r).

For a given the sequence A={a1,a2,…,an}, define the weight of a sequence B={b1,b2,…,bn} be ∑i=1nbi (i.e. the sum of all elements in B) if sequence B and sequence A are RMQ Similar, or 0 otherwise. If each element of B is a real number chosen independently and uniformly at random between 0 and 1, find the expected weight of B.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤106) – the length of the sequence.
The second line contains n integers a1,a2,…,an (1≤ai≤n) denoting the sequence.
It is guaranteed that the sum of all n does not exceed 3×106.

Output

For each test case, output the answer as a value of a rational number modulo 109+7.
Formally, it is guaranteed that under given constraints the probability is always a rational number pq p q (p and q are integer and coprime, q is positive), such that q is not divisible by 1e9+7. Output such integer a between 0 and 1e9+6 that p−aq is divisible by 1e9+7.

Sample Input

3
3
1 2 3
3
1 2 1
5
1 2 3 2 1

Sample Output

250000002
500000004
125000001

Source

2018 Multi-University Training Contest 1


这题现场被卡常了,3e6的nlogn竟然会T,吐槽HDU的评测机
我们发现对于相同的数,RMQ的位置算最左边的那个,相当于对于相同的数,越靠左越大,这样我们可以对A数组进行重新标号,这样可以得到一个和他similar的一个n的排列A
考虑到在0到1之间随机n个数,有数字相等的概率是0,所以可以认为把随机出来的数离散化以后一定是一个n的排列,所以随机到每种排列的概率应该都是

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值