LightOj_1027 A Dangerous Maze

题目链接

题意:

  你在一个迷宫里, 开始的时候你面前有n个门, 选择每个门的概率相等, 有两种结果:

  1)回到|x|分钟之前(x为负时)

  2)x分钟之后出迷宫(x为正时)

  每次回到|x|分钟之前, 你都记不得你曾经选过哪扇门

  问走出迷宫所用时间的期望。

思路:

  因为每次都不记得曾经的选择, 所以每次的期望都是一样的。

  设,T1为每次走出去所用时间的期望, T2为回到之前所用时间的期望。

  则E = p * T1 + (T2 + E) * (1 - p)

  化简后得, p * E = p * T1 + T2 * (1 - p)

  假设有m个门是走出去的, 则p = m / n

  代进上式得:m * E / n = m * T1 / n + (n - m) * T2 / n

          m * E = m * T1 + (n - m) * T2

  T1 = (sigma x[i]) / m, x[i] > 0, T2 = (sigma |x[i]|) / (n - m), x[i] < 0

  E = (sigma x[i] + sigma |x[i]|) / m

代码:

  

 1 #include <cmath>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <ctime>
 6 #include <set>
 7 #include <map>
 8 #include <list>
 9 #include <queue>
10 #include <string>
11 #include <vector>
12 #include <fstream>
13 #include <iterator>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 #define LL long long
18 #define MAXN 110
19 #define MOD 1000000007
20 #define eps 1e-6
21 int n;
22 int t[2], m;
23 int gcd(int a, int b)
24 {
25     return b == 0? a : gcd(b, a % b);
26 }
27
28 int main()
29 {
30     int T;
31     int kcase = 0;
32     scanf("%d", &T);
33     while(T --)
34     {
35         scanf("%d", &n);
36         m = 0;
37         t[0] = t[1] = 0;
38         for(int i = 0; i < n; i ++)
39         {
40             int x;
41             scanf("%d", &x);
42             if(x > 0)
43             {
44                 t[0] += x;
45                 m ++;
46             }
47             else
48                 t[1] += (-1 * x);
49         }
50         if(!m) printf("Case %d: inf\n", ++ kcase);
51         else
52         {
53             int k = gcd(t[0] + t[1], m);
54             printf("Case %d: %d/%d\n", ++ kcase, (t[0] + t[1]) / k, m / k);
55         }
56     }
57     return 0;
58 }

时间: 2024-08-25 22:59:30

LightOj_1027 A Dangerous Maze的相关文章

lightoj-1027 - A Dangerous Maze(数学期望)

1027 - A Dangerous Maze PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 32 MBYou 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 do

[LOJ 1027] Dangerous Maze

A - A Dangerous Maze Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description 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

[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 positio

light OJ 1027 A Dangerous Maze (期望)

1027 - A Dangerous Maze PDF (English) Statistics Forum 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

(期望)A Dangerous Maze(Light OJ 1027)

http://www.lightoj.com/volume_showproblem.php?problem=1027 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 ca

LightOJ1027---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

LightOJ1027 A Dangerous Maze(期望)

题目大概说你正在起点,面前有$n$个门,每个门有一个数字$x$,正数表示开这个门$x$分钟后会出去,负数表示开这个门$-x$分钟后会回到起点.选择门的概率是一样的且每次选择互不影响.问出去的时间期望是多少. $d[0]$表示从起点出发出去的期望,$d[i](1\leqslant i\leqslant n)$表示选择第i个门出去的期望: 不妨设$1\dots j$的门是正数,$j+1\dots n$的门是负数,那么: $$d[i]=x[i](1\leqslant i\leqslant j)$$ $

Light OJ 1027 - A Dangerous Maze (数学-期望)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1027 题目大意: 一个迷宫, 有n个门,选择一个门花费为|ai|, 如果选择的门是正数, 那么直接走出迷宫, 否则重新回到起始位置.选择每一道门的概率是一样的.求走出迷宫的花费的期望. 解题思路:n个门中正数的门有s个, 那么一次选择出去的概率为s/n, 那么出去需要次数的期望为n/s. 对于每一次选择, 需要花费的平均时间为sum(|ai|)/n, 那么走出迷宫的花费的期望

LightOJ 1027 Dangerous Maze

经典概率,主要找递推式. 给你n个门,每次选一个,如果为正x就x秒后结束,否则-x秒后还要留在这里,求期望. ANS=P_POS*POS_AVERAGE+P_NEG*(NEG_AVERAGE+ANS); 借出Y即可. #include <iostream> #include <functional> #include <algorithm> #include <complex> #include <cstdlib> #include <cs