ZOJ 3929 Deque and Balls (放球问题_DP思维好题)

探讨在一个双端队列中按顺序放置标记球时,如何计算下降位置的期望值。通过对动态规划的理解,找到每一步放置球所带来的贡献,并最终得出整体的期望值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


题目大意:给出一个序列,按照顺序一个一个放入双端队列(可以放在头部也可以放在尾部)问,xi>xi+1的期望是多少?

思路:有点动态规划的味道,已知前面i个都放好了,那么下一个i+1个放进了会造成多少的贡献,因为第i+1个可以放前可以放后,

那么ans*2,再考虑第i+1个数与其他数字造成的贡献。。。此时你会找到回来,a1,a2两次,之后a3=4,a4=8.....

so....ans就显而易见了....呵呵


There are n balls, where the i-th ball is labeled as pi. You are going to put n balls into a deque. In the i-th turn, you need to put the i-th ball to the deque. Each ball will be put to both ends of the deque with equal probability.

Let the sequence (x1x2, ..., xn) be the labels of the balls in the deque from left to right. The beauty of the deque B(x1x2, ..., xn) is defined as the number of descents in the sequence. For the sequence (x1x2, ..., xn), a descent is a position i (1 ≤ i < n) with xi > xi+1.

You need to find the expected value of B(x1x2, ..., xn).

Deque is a double-ended queue for which elements can be added to or removed from either the front (head) or the back (tail).

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 (2 ≤ n ≤ 100000) -- the number of balls. The second line contains n integers: p1p2, ..., pn (1 ≤ pi ≤ n).

Output

For each test case, if the expected value is E, you should output E⋅2n mod (109 + 7).

Sample Input
2
2
1 2
3
2 2 2
Sample Output
2
0











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值