【ZOJ3640】Help Me Escape

· · 个人记录

Help\ Me\ Escape

ZOJ - 3640

题意

一个光~头~掉到了一个洞里,洞里有n条路,第i条路的难度是c_i,光头一开始有一个能力值f,若f此时大于等于c_i,光头可以花\lfloor \frac{1+\sqrt5}2{c_i^2}\rfloor天走过这条路;不然光头会磨炼1天,并且增加c_i能力值。

求光头钻出来的期望值(老葛平了

Analysis

我们用记搜做

P[x]$表示能力为$x$钻出来的期望天数,如果$x\leq c_i$,$P[x]$加上$dp(x+c[i])+\frac{1}{n}$;如果$x>c_i$,$P[x]$加上$\lfloor \frac{1+\sqrt5}2{c_i^2}\rfloor
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
template<class T>
void read(T &x)
{
    T ans=0;short f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }while(ch>='0'&&ch<='9')
    {
        ans=(ans<<1)+(ans<<3)+(ch^48);
        ch=getchar();
    }x=ans*f;
}

#define maxn 200010
int n,f;
int c[10001];
double P[maxn];
const double p=1.61803398874989;
double dp(int x){
    if(P[x]>-0.5)return P[x];
    P[x]=0;
    for(int i=1;i<=n;i++)
    if(x<=c[i])P[x]+=double(dp(x+c[i])+1.0)/n;
    else P[x]+=int(p*c[i]*c[i])/(double)n;
    return P[x];
}
signed main(){
#ifndef ONLINE_JUDGE
    freopen("testdata.in","r",stdin);
#endif
    while(scanf("%d %d",&n,&f)!=EOF){
        for(int i=0;i<=200000;i++)P[i]=-1;
        for(int i=1;i<=n;i++)read(c[i]);
        printf("%.3lf\n",dp(f));
    }
}