Aizu 2309 Sleeping Time DFS
Sleeping Time
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=93265#problem/BDescription
Miki is a high school student. She has a part time job, so she cannot take enough sleep on weekdays. She wants to take good sleep on holidays, but she doesn't know the best length of sleeping time for her. She is now trying to figure that out with the following algorithm:
- Begin with the numbers K, R and L.
- She tries to sleep for H=(R+L)/2 hours.
- If she feels the time is longer than or equal to the optimal length, then update L with H. Otherwise, update R with H.
- After repeating step 2 and 3 for K nights, she decides her optimal sleeping time to be T' = (R+L)/2.
If her feeling is always correct, the steps described above should give her a very accurate optimal sleeping time. But unfortunately, she makes mistake in step 3 with the probability P.
Assume you know the optimal sleeping time T for Miki. You have to calculate the probability PP that the absolute difference of T' and T is smaller or equal to E. It is guaranteed that the answer remains unaffected by the change of E in 10^{-10}.
Input
The input follows the format shown below
KRL
P
E
T
Where the integers 0 \leq K \leq 30, 0 \leq R \leq L \leq 12 are the parameters for the algorithm described above. The decimal numbers on the following three lines of the input gives the parameters for the estimation. You can assume 0 \leq P \leq 1, 0 \leq E \leq 12, 0 \leq T \leq 12.
Output
Output PP in one line. The output should not contain an error greater than 10^{-5}.
Sample Input
3 0 2 0.10000000000 0.50000000000 1.00000000000
Sample Output
0.900000
HINT
题意
有个人要睡觉,他的睡觉是二分时间睡的……
他有p的概率二分错,然后问你他有多少的概率二分正确
题解:
就dfs就好了,类似线段树区间查询一样,可以在中间直接break的
代码:
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <bitset> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; int n;double L,R,P,E,T; double ans = 0; void dfs(int now,double l,double r,double p,double e,double t) { if(l>(T+E)&&(r>(T+E))) return; if(l<(T-E)&&r<(T-E)) return; if(l<=(T+E)&&l>=(T-E)&&r<=(T+E)&&r>=(T-E)) { ans+=p; return; } double h = (l+r)/2.0; if(now == n) { if(fabs(h-T)<=E) ans += p; return; } if(h-T>=-1e-10) { dfs(now+1,h,r,p*(P),e,t); dfs(now+1,l,h,p*(1-P),e,t); } else { dfs(now+1,l,h,p*(P),e,t); dfs(now+1,h,r,p*(1-P),e,t); } } int main() { scanf("%d",&n); scanf("%lf%lf%lf%lf%lf",&L,&R,&P,&E,&T); dfs(0,L,R,1,E,T); printf("%.10lf\n",ans); return 0; }