Description
Now Cain is unexpectedly trapped in a cave with N paths. Due to LORD's punishment, all the paths are zigzag and dangerous. The difficulty of the ith path is ci.
Then we define f as the fighting capacity of Cain. Every day, Cain will be sent to one of the N paths randomly.
Suppose Cain is in front of the ith path. He can successfully take ti days to escape from the cave as long as his fighting capacity f is larger than ci. Otherwise, he has to keep trying day after day. However, if Cain failed to escape, his fighting capacity would increase cias the result of actual combat. (A kindly reminder: Cain will never died.)
As for ti, we can easily draw a conclusion that ti is closely related to ci. Let's use the following function to describe their relationship:
After D days, Cain finally escapes from the cave. Please output the expectation of D.
题意:
给出N条逃跑路径,每条路径有一个困难值Ci,Cain要逃跑,一开始其有战斗值f,他可以花费一个单位的时间来等概率随机选择其中一条路径逃跑,如果其战斗值f大于Ci,则在经过Ti时间(公式如上)后成功,如果小于等于,则失败(不会多花时间),且战斗值f增长Ci,问逃跑所需要的时间的期望。
范围:
N<=100,f<=1000000,Ci<=10000
解法:
发现最后逃跑时,只要f大于最大的一个Ci(记为M-1),必定可以逃跑,则可以考虑DP,DP[I]表示当前战斗力为I,还需要期望花费DP[I]才能成功逃跑,可知DP[M]=Σ1/N * Ti
确定了边界条件后,转移方程就很简单了:
DP[I]=1/N* ( ΣTj +Σ(dp[K]+1) ) 其中J为当前战斗力I已经可以逃生的路径,K为还不可逃生的路径,注意转移时不要越界。
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret){ //Faster Input
char c; int sgn; T bit=0.1;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
if(c==' '||c=='\n'){ ret*=sgn; return 1; }
while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
ret*=sgn;
return 1;
}
#define inf 1073741823
#define llinf 4611686018427387903LL
#define PI acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)
#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)
#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)
#define mem(x,val) memset(x,val,sizeof(x))
#define mkp(a,b) make_pair(a,b)
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
double dp[10010];
int c[10010];
int n,m,f;
double s=(sqrt(5.0)+1.0)/2.0;
int main(){
while(scanf("%d",&n)!=EOF){
scanff(f);
m=-inf;
rep(i,1,n){
scanff(c[i]);
m=max(m,c[i]);
}
m++;
dp[m]=0;
rep(i,1,n){
dp[m]+=floor(s*double(c[i]*c[i]));
}
dp[m]/=double(n);
drep(i,m-1,f){
dp[i]=0;
rep(j,1,n){
if(c[j]<i)dp[i]+=floor(s*double(c[j]*c[j]));
else{
int k=min(i+c[j],m);
dp[i]+=dp[k]+1;
}
}
dp[i]/=n;
}
printf("%.3lf\n",dp[f]);
}
}