博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LightOJ - 1027 A Dangerous Maze —— 期望
阅读量:5318 次
发布时间:2019-06-14

本文共 2770 字,大约阅读时间需要 9 分钟。

题目链接:

 

1027 - A Dangerous Maze
  
Time Limit: 2 second(s) Memory Limit: 32 MB

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 xi minutes, 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 ithdoor 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

Output for Sample Input

3

 

1

1

 

2

-10 -3

 

3

3 -6 -9

Case 1: 1/1

Case 2: inf

Case 3: 18/1

 

 

题意:

有n扇门,一扇门要么能在特定时间内把人带出迷宫,要么能在特定时间内把人带会原地,并使人失去记忆(即不知道这扇门是不能带出迷宫的),每扇门被选择的几率是相等的。问走出迷宫的平均时间。

 

题解:

设期望为EX,有t1扇“正门”,t2扇“负门”,其中t1+t2 = n 。第i扇门所花费的时间为ai

1.当选择的门为正门时,它可以直接走出迷宫,因此:1/n*ai,1/n为选择这扇门的几率,且所有的门被选择的几率也为1/n。

2.当选择的门为负门时,它先花费了ai的时间,然后又重新选择,重新选择然能走出迷宫的时间即为平均时间,因此:1/n*(ai+EX)。

3.综上,EX = ∑1/n*ai + ∑1/n*(ai+EX),其中第一个i的范围为:1<=i<=t1,第二个i:1<=i<=t2。

 

代码如下:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 using namespace std;13 typedef long long LL;14 const int INF = 2e9;15 const LL LNF = 9e18;16 const int MOD = 1e9+7;17 const int MAXN = 1e6+100;18 19 int gcd(int a, int b)20 {21 return b==0?a:gcd(b,a%b);22 }23 24 int main()25 {26 int T, n, kase = 0;27 scanf("%d", &T);28 while(T--)29 {30 scanf("%d", &n);31 int numP = 0, sum = 0;32 for(int i = 1; i<=n; i++)33 {34 int val;35 scanf("%d", &val);36 sum += abs(val);37 if(val>0) numP++;38 }39 if(numP==0)40 printf("Case %d: inf\n", ++kase);41 else42 printf("Case %d: %d/%d\n", ++kase, sum/gcd(sum, numP), numP/gcd(sum, numP));43 }44 }
View Code

 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/8442427.html

你可能感兴趣的文章
C++ 删除字符串的两种实现方式
查看>>
ORA-01502: 索引'P_ABCD.PK_WEB_BASE'或这类索引的分区处于不可用状态
查看>>
Java抽象类和接口的比较
查看>>
MyBaits学习
查看>>
管道,数据共享,进程池
查看>>
CSS
查看>>
[Cypress] Stub a Post Request for Successful Form Submission with Cypress
查看>>
UNITY在VS中调试
查看>>
SDUTOJ3754_黑白棋(纯模拟)
查看>>
Scala入门(1)Linux下Scala(2.12.1)安装
查看>>
如何改善下面的代码 领导说了很耗资源
查看>>
php中的isset和empty的用法区别
查看>>
Android ViewPager 动画效果
查看>>
把word文档中的所有图片导出
查看>>
浏览器的判断;
查看>>
ubuntu 18.04取消自动锁屏以及设置键盘快捷锁屏
查看>>
Leetcode 589. N-ary Tree Preorder Traversal
查看>>
机器学习/深度学习/其他开发环境搭建记录
查看>>
xml.exist() 实例演示
查看>>
判断是否为空然后赋值
查看>>