ZOJ 3604 Help Me Escape(期望dp)
阿新 • • 發佈:2020-12-17
技術標籤:概率期望、概率期望dp
題目傳送門
題意: 有n條道路,每條道路有一個危險值 c [ i ] c[i] c[i],你有一個初始戰鬥力 f f f,如果你的戰鬥力大於一條道路的戰鬥力,那麼你需要花 1 + 5 2 ∗ c [ i ] 2 \frac{1+\sqrt5}{2}*c[i]^2 21+5 ∗c[i]2天逃離,否則,你每天戰鬥力增加 c [ i ] c[i] c[i]。現在每天給你隨機扔到一條道路上,直到你逃離這條路再繼續下一條路。問你逃離所有道路的期望天數。
思路:
d
p
[
f
]
dp[f]
dp[f]表示戰鬥力為
f
f
f時,還需要
d
p
[
f
]
dp[f]
dp[f]天才能逃離,直接記憶化搜尋即可。
程式碼:
#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define int long long
#define lowbit(x) x&-x
#define pii pair<int,int>
#define ull unsigned long long
#define pdd pair<double,double>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read()
{
int x=0,f=1;
char ch=gc();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=gc();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=gc();
}
return x*f;
}
using namespace std;
const int N=4e2+666;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-7;
const double PI=acos(-1);
double dp[20010];
int c[N],n;
double dfs(int f)
{
if(dp[f]>0)
return dp[f];
dp[f]=0;
for(int i=1;i<=n;i++)
{
if(f>c[i])
{
double temp=(1.0+sqrt(5))/2*c[i]*c[i];
int t = (int)temp;
dp[f]+=(double)t/n;
}
else
{
dp[f]+=(dfs(f+c[i])+1)/n;
}
}
return dp[f];
}
int solve()
{
int f;
while(cin>>n>>f)
{
memset(dp,0,sizeof dp);
for(int i=1; i<=n; i++)
{
cin>>c[i];
}
printf("%.3lf\n",dfs(f));
}
return 0;
}
signed main()
{
// int _;
// cin>>_;
// while(_--)
solve();
return 0;
}