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

### 关于2024 ICPC网络预选赛的信息 #### 参规则 国际大学生程序设计竞ICPC)作为一项全球性的事,其参规则通常保持一定的稳定性。根据以往的经验和惯例[^1],每支参队伍由三名队员组成,他们需在同一台计算机上合作完成一系列算法问题的解答。比期间不允许使用任何外部资源或工具书,仅能依赖团队成员的知识储备以及现场提供的少量参考资料。 对于具体的2024年ICPC网络预选赛而言,虽然官方尚未发布详细的最新规定,但可以推测基本框架不会发生显著变化。例如,在线形式可能继续沿用近年来因疫情而普及的方式;同时也会严格监控作弊行为以维护公平竞争环境[^2]。 #### 时间安排 关于2024年的具体时间表目前还没有确切消息公布出来。然而按照传统模式来看,整个流程一般会经历以下几个阶段: - **报名期**:预计会在年初开放注册通道供各高校提交候选名单。 - **区域选拔/网络挑战**:这通常是多轮次举行,覆盖不同大洲和地区的时间段以便更多选手参与进来体验实战氛围并争取晋级机会。 - **全球总决准备阶段**:成功突围进入最终环节者将获得额外培训指导来提升实力迎接巅峰对决时刻。 以下是基于历史数据猜测的一个大致日程示例(请注意实际日期应参照官方通知为准): | 阶段 | 开始日期 | 结束日期 | |--------------------|---------------|----------------| | 报名截止 | 2024-02-15 | | | 初步筛选结果公告 | | 2024-03-01 | | 第一轮网络资格 | 2024-03-10 | 2024-03-12 | | 复活 | 2024-04-07 | 2024-04-09 | | 半决分区 | 各区自行决定 | 各区自行决定 | #### 题目解析 由于正式试题还未出炉之前无法提供针对性分析,不过可以根据往届经典案例来进行一些通用技巧分享: ```python def example_problem(): """ 假设有一道简单的字符串匹配问题. 给定两个长度不超过1e6 的字符串S 和T(S >= T),判断是否存在子串使得两者完全一致. 如果存在返回True;否则False. """ from collections import defaultdict def kmp_preprocess(pattern): lps = [0]*len(pattern) j=0;i=1; while i<len(pattern): if pattern[i]==pattern[j]: lps[i]=j+1;j+=1;i+=1; elif j!=0: j=lps[j-1]; else : lps[i]=0;i+=1 return lps; s="abcde";t="bcd"; lps=kmp_preprocess(t); q=[]; res=False; for char in s: while(q and (not t[len(q)]==char)): last=len(q)-lps[-1];q=q[:last]; if not q or t[len(q)]==char:q.append(char); if ''.join(q)==t:res=True;break; print(res) example_problem() ``` 上述代码片段展示了一个利用KMP算法解决字符串查找的经典方法之一。当然这只是众多可能性中的很小一部分而已。 ---
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值