ZOJ - 3640 概率DP
题意:给定初始值为f,等概率相遇n种怪物,其相应值的c[i],若当前值大于c[i],达成的天数为t[i],否则花一天升级c[i]再t[i]达成,求达成期望
注意到过程是逆序单调的,先初始化Limit再倒过来递推即可
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 66666+11;
const double eps = 1e-7;
typedef long long ll;
const int oo = 0x3f3f3f3f;
ll read(){
ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
double dp[maxn];//zhandouliwei i shi taoli de qiwang
// double dp[maxn];//d
//if manzu dp[i]=dp[i]+p t[i]
//else bumanzu dp[i]=p dp[i+ci]+1
int n,f,c[maxn],t[maxn];
int main(){
while(cin>>n>>f){
rep(i,1,n) c[i]=read();
rep(i,1,n) t[i]=(int)((1.0+sqrt(5))/2.0*c[i]*c[i]);
memset(dp,0,sizeof dp);
int Limit=-1;
ll sum=0;
rep(i,1,n){
sum+=t[i];
Limit=max(Limit,c[i]+1);
}
rep(i,Limit,Limit<<1)dp[i]=(double)sum/n;
rrep(i,Limit-1,f) rep(j,1,n){
if(i>c[j]) dp[i]+=(double)1.0/n*t[j];
else dp[i]+=(double)(1.0/n*(1+dp[i+c[j]]));
}
printf("%.3lf\n",dp[f]);
}
return 0;
}