[LightOJ 1027] A Dangerous Maze
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LightOJ 1027] A Dangerous Maze相关的知识,希望对你有一定的参考价值。
A Dangerous Maze
You are in a maze; seeing n doors in front of you in beginning. You can choose any door you like. The probability for choosing a door is equal for all doors.
If you choose the ith door, it can either take you back to the same position where you begun in ximinutes, or can take you out of the maze after xi minutes. If you come back to the same position, you can‘t remember anything. So, every time you come to the beginning position, you have no past experience.
Now you want to find the expected time to get out of the maze.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains a blank line and an integer n (1 ≤ n ≤ 100) denoting the number of doors. The next line contains n space separated integers. If the ith integer (xi) is positive, you can assume that the ith door will take you out of maze after xi minutes. If it‘s negative, then the ith door will take you back to the beginning position after abs(xi) minutes. You can safely assume that 1 ≤ abs(xi) ≤ 10000.
Output
For each case, print the case number and the expected time to get out of the maze. If it‘s impossible to get out of the maze, print ‘inf‘. Print the result in p/q format. Where p is the numerator of the result and q is the denominator of the result and they are relatively prime. See the samples for details.
Sample Input
3
1
1
2
-10 -3
3
3 -6 -9
Sample Output
Case 1: 1/1
Case 2: inf
Case 3: 18/1
题目的大意是,有n扇门,一些会在xi秒后带你立刻迷宫,一些会让你回到-xi秒前,选择每扇门的概率相同,求离开迷宫所需时间的期望.
这是期望数学的入门题.
我们设出去的期望为E,选正数的概率为P1,之后平均花T1的时间出去;选负数的概率是P2,之后平均花T2的时间出去。
则E=P1*T1+P2*(T2+E);
解得:E=(P1T1+P2T2)/ (1-P2)=(P1T1+P2T2)/ P1
我们再设正权和为S1,负权和为S2,正权数为N1,负权数为N2,总数为N,则:
T1*N1/N+T2*N2/N (S1+S2)/N S1+S2
E=-----------------------------=----------------=----------
N1/N N1/N N1
如果N1为0,那永远走不出去,输出inf就行了.
问题就解决了.
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int n; 6 int gcd(int x,int y){return y==0?x:gcd(y,x%y);} 7 int main(){ 8 int T; scanf("%d",&T); 9 for (int Ts=1; Ts<=T; Ts++){ 10 scanf("%d",&n); 11 int sum_pos=0,sum_neg=0,num_pos=0,num_neg=0; 12 for (int i=1; i<=n; i++){ 13 int x; scanf("%d",&x); 14 if (x>0) sum_pos+=x,num_pos++; else sum_neg-=x,num_neg++; 15 } 16 printf("Case %d: ",Ts); 17 if (num_pos==0){printf("inf\n"); continue;} 18 int x=sum_pos+sum_neg,y=n-num_neg,K=gcd(x,y); 19 printf("%d/%d\n",x/K,y/K); 20 } 21 return 0; 22 }
以上是关于[LightOJ 1027] A Dangerous Maze的主要内容,如果未能解决你的问题,请参考以下文章
LightOJ1027 A Dangerous Maze(期望)
[LightOJ 1027] A Dangerous Maze
LightOJ 1027 A Dangerous Maze(期望)