多校5 1001 HDU5781 ATM Mechine 记忆化搜索+概率

 1 // 多校5 1001 HDU5781 ATM Mechine
 2 // http://acm.hdu.edu.cn/search.php?field=problem&key=2016+Multi-University+Training+Contest+5&source=1&searchmode=source
 3 // 记忆化搜索
 4 // 每次二分,决策最优,所以最多查询11次
 5 // dp[i][j] 当前能确定i元情况下,还能被警告j次
 6 // 下次取k,实际剩余t
 7 // dp[i][j]=min(1≤k≤i)({(∑(0≤t<k)dp[k?1][j-1]+∑(k≤t≤i)dp[i?k][j])/(i+1)})+1=(min(1≤k≤i){k*dp[k?1][j-1]+(i?k+1)*dp[i?k][j]})/(i+1)+1
 8 // k=0 没有钱可以取,当然次数为0
 9 // w=0 这种情况要排除
10 // w=1 最后只能1块的取,直到取完,警告没有钱可以取了
11 
12 
13 // #pragma comment(linker, "/STACK:102c000000,102c000000")
14 #include <iostream>
15 #include <cstdio>
16 #include <cstring>
17 #include <sstream>
18 #include <string>
19 #include <algorithm>
20 #include <list>
21 #include <map>
22 #include <vector>
23 #include <queue>
24 #include <stack>
25 #include <cmath>
26 #include <cstdlib>
27 // #include <conio.h>
28 using namespace std;
29 #define clc(a,b) memset(a,b,sizeof(a))
30 const double inf = 0x3f3f3f3f;
31 #define lson l,mid,rt<<1
32 // #define rson mid+1,r,rt<<1|1
33 const int N = 2010;
34 const int M = 1e6+10;
35 const int MOD = 1e9+7;
36 #define LL long long
37 #define LB long double
38 // #define mi() (l+r)>>1
39 double const pi = acos(-1);
40 const double eps = 1e-8;
41 void fre(){freopen("in.txt","r",stdin);}
42 inline int read(){int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;}
43 
44 double dp[2010][11];
45 bool vis[2010][11];
46 double dfs(int k,int x){
47     double ans=inf;
48     if(k==0) return 0;
49     if(x==0) return inf;
50     if(x==1) return (double)((1 + k + 1) * (k + 1) / 2 - 1) / (k + 1);
51     if(vis[k][x]) return dp[k][x];
52     for(int i=1;i<=k;i++){
53        ans=min(ans,1.0*(k-i+1)/(k+1)*dfs(k-i,x)+1.0*i/(k+1)*dfs(i-1,x-1)+1);
54     }
55     vis[k][x]=true;
56     return dp[k][x]=ans;
57 }
58 
59 int main(){
60     int k,w;
61     while(~scanf("%d%d",&k,&w)){
62         double ans=dfs(k,min(w,11));
63         printf("%.6f
",ans);
64     }
65     return 0;
66 }
原文地址:https://www.cnblogs.com/ITUPC/p/5734170.html