The 2023 ICPC Asia Regionals Online Contest (2)

I Impatient Patient

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(0≤a i ≤i). The probability of success is p iNow, 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≤10 
4
 ), denoting the number of test cases.

For each test case:

The first line contains one integer n (1≤n≤5×10 
5
 ), denoting the number of stages of recovery.
The next line contains n integers a 
0

 ,a 
1

 ,⋯,a 
n−1

  (0≤a 
i

 ≤i), denoting the stage you will go back to if you fail at stage i.
The next line contains n integers q 
0

 ,q 
1

 ,⋯,q 
n−1

  (0≤q 
i

 ≤10 
5
 ), where p 
i

 =q 
i

 /10 
5
  denotes the probability of success at stage i.
It is guaranteed that ∑n≤5×10 
5
 .

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
5
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
1.000000000000
1.000010000100
2.999960000800
4.447607187333
7.096039133935

思路:每一天都有两种选择,休息或是挑战,要使时间最短就只能不挑战或一直挑战,如果挑战一次又不挑战,那么从跳回去的地方回到原位置就会增加时间。

对于第i个阶段,它所需的时间期望为i+((1-q[i])/q[i])*(i-a[i]+1)+1,(1-q[i])/q[i]为一直挑战的概率,

(i-a[i]+1)为一直挑战增加的时间。对时间遍历取最小值。

代码:

#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n],b[n],q[n];
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        for(int i=1;i<=n;i++){
            cin>>b[i];
            q[i]=b[i]/1e5;
        }
        double idx,ans=n;
        for(int i=1;i<=n;i++){
            idx=i+((1-q[i])/q[i])*(i-a[i]+1)+1;
            ans=min(ans,idx);
        }
        printf("%.12lf\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值