概率dp专场

 

专题链接

第一题--poj3744 Scout YYF I  链接 (简单题)

算是递推题 如果直接推的话 会TLE 会发现 在两个长距离陷阱中间 很长一部分都是重复的 我用 a表示到达i-2步的概率 b表示到达i-1步的概率 c表示到达i步的概率

如果数很大的话 中间肯定会有重复的a,b,c 直接将i挪到最近的陷阱前一位 i = a[o]-1,大大节省时间。

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<stdlib.h>
 6 #include<vector>
 7 #include<cmath>
 8 #include<queue>
 9 #include<set>
10 using namespace std;
11 #define LL long long
12 #define INF 0xfffffff
13 const double eps = 1e-8;
14 const double pi = acos(-1.0);
15 const double inf = ~0u>>2;
16 int x[12];
17 int main()
18 {
19     int i,n,j;
20     double p;
21     while(cin>>n)
22     {
23         cin>>p;
24         for(i = 0 ;i < n ;i++)
25         {
26             cin>>x[i];
27         }
28         sort(x,x+n);
29         double a = 1.0,b=0.0,c;
30         double ta = 0,tb = 0;
31         int o = 0;
32         if(x[0]==1)
33         a = 0;
34         else a = 1;
35         for(i = 2; i <= x[n-1]+1 ; i++)
36         {
37             if(i==x[o])
38             {
39                 o++;
40                 while(x[o]==x[o-1])
41                 o++;
42                 c = 0;
43             }
44             else
45             c = a*p+b*(1.0-p);
46             b = a;
47             a = c;
48             if(o<n&&fabs(ta-a)<eps&&fabs(tb-b)<eps)
49             i = x[o]-1;
50             ta = a;
51             tb = b;
52         }
53         printf("%.7f\n",c);
54     }
55     return 0;
56 }
View Code

相关文章:

  • 2021-11-15
  • 2021-05-30
  • 2021-11-11
  • 2021-09-26
  • 2022-01-18
  • 2022-12-23
猜你喜欢
  • 2021-10-27
  • 2022-12-23
  • 2022-01-17
  • 2021-10-08
相关资源
相似解决方案