ICPC网络预选赛第二场个人题解

文章讲述了在康复期间如何利用挑战策略和概率计算来最小化期望恢复时间,涉及数学模型和代码实现。


目前只更新了I题,别的有空补上。

I Impatient Patient

You accidentally sprained your ankle, and now you are facing a long recovery phase. Initially, you are at stage 0, and your recovery progresses until you reach stage n.

Each day, if you rest properly, you advance by exactly one stage. So it takes n days for you to recover, that is, if you do not do anything improper.

However, instead of resting, you have the option to challenge yourself, which also takes one day. If you are at stage i and succeed, you will instantly recover. However, if you fail, you will regress to stage a i a_i ai (0≤ a i a_i aii). The probability of success is p i p_i pi.

Now, you are wondering what the expected time required for your recovery would be, assuming you adopt the best strategy.

Input

The first line contains a positive integer T (1≤T 1 0 4 10^4 104), denoting the number of test cases.

For each test case:

  • The first line contains one integer n (1≤n≤5× 1 0 5 10^5 105), denoting the number of stages of recovery.
  • The next line contains n integers a 0 a_0 a0, a 1 a_1 a1,⋯, a n − 1 a_{n-1} an1 (0≤ a i a_i aii), denoting the stage you will go back to if you fail at stage i.
  • The next line contains n integers q 0 q_0 q0, q 1 q_1 q1,⋯, q n − 1 q_{n-1} qn1 (0≤ q i q_i qi≤105), where p i p_i pi= q i q_i qi/ 1 0 5 10^5 105 denotes the probability of success at stage i.

It is guaranteed that ∑n≤5× 1 0 5 10^5 105.

Output

For each test case, print one decimal number, denoting the expected time needed for you to recover if you take the best strategy. The answer will be considered correct if its absolute or relative error is less than 10^-9.

Sample Input

inCopy code5
1
0
0
3
0 1 2
99999 0 0
3
0 1 2
0 50001 100000
5
0 1 2 0 1
21735 25194 37976 89936 99999
8
0 0 2 2 4 3 4 1
12839 27084 17777 35472 31951 64686 96898 0

Sample Output

outCopy code1.000000000000
1.000010000100
2.999960000800
4.447607187333
7.096039133935

Code Size Limit

64 KB

Time Limit

1000 ms

Memory Limit

512 MB

翻译:

你不小心扭伤了脚踝,现在面临着漫长的康复期。最初,你处于第 0 0 0阶段,康复会一直进行,直到达到第 n n n阶段。

每天,如果你适当休息,你会前进一阶段。所以如果你不采取任何不当行动,你需要n天来康复。

然而,除了休息,你还有挑战自己的选项,这也需要一天的时间。如果你处于第 i i i阶段并且成功了,你将立刻康复。但是,如果失败了,你将回到第 a i a_i ai 阶段 ( 0 ≤ a i ≤ i ) (0 \leq a_i \leq i) 0aii)。成功的概率是 p i p_i pi

现在,你想知道,假设你采取最佳策略,需要多长时间才能康复。

输入

第一行包含一个正整数 T ( 1 ≤ T ≤ 1 0 4 ) T(1≤T≤10^4) T(1T104),表示测试用例的数量。

对于每个测试用例:

第一行包含一个整数 n ( 1 ≤ n ≤ 5 ⋅ 1 0 5 ) n(1 \leq n \leq 5 \cdot 10^5) n1n5

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值