首页 > 代码库 > ZOJ3640之简单慨率DP

ZOJ3640之简单慨率DP

Help Me Escape

Time Limit: 2 Seconds      Memory Limit: 32768 KB

Background

    If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at the door. And unto thee shall be his desire, and thou shalt rule over him. 
    And Cain talked with Abel his brother: and it came to pass, when they were in the field, that Cain rose up against Abel his brother, and slew him. 
    And the LORD said unto Cain, Where is Abel thy brother? And he said, I know not: Am I my brother‘s keeper? 
    And he said, What hast thou done? the voice of thy brother‘s blood crieth unto me from the ground. 
    And now art thou cursed from the earth, which hath opened her mouth to receive thy brother‘s blood from thy hand; 
    When thou tillest the ground, it shall not henceforth yield unto thee her strength; a fugitive and a vagabond shalt thou be in the earth.

—— Bible Chapter 4

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 ci as 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.

Input

The input consists of several cases. In each case, two positive integers N and f (n ≤ 100, f ≤ 10000) are given in the first line. The second line includes N positive integers ci (ci ≤ 10000, 1 ≤ i ≤ N)

Output

For each case, you should output the expectation(3 digits after the decimal point).

Sample Input

3 1
1 2 3

Sample Output

6.889

题意:

dfs记忆话搜索方式:

/*题意:
一只吸血鬼,有n条路给他走,每次他随机走一条路,
每条路有个限制,如果当时这个吸血鬼的攻击力大于
等于某个值,那么就会花费t天逃出去,否则,花费1天
的时间,并且攻击力增加,问他逃出去的期望 
*/ 
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=20000+10;
int n,f,c[MAX];
double dp[MAX],p;//dp[i]表示战斗力为i时出去的期望

double dfs(int f){
	if(dp[f]>0)return dp[f];
	for(int i=1;i<=n;++i){
		if(f>c[i])dp[f]+=(int)(p*c[i]*c[i])*1.0/n;
		else dp[f]+=(dfs(f+c[i])+1)*1.0/n;
	}
	return dp[f];
}

int main(){
	p=(1.0+sqrt(5))/2.0;
	while(~scanf("%d%d",&n,&f)){
		memset(dp,0,sizeof dp);
		for(int i=1;i<=n;++i)scanf("%d",&c[i]);
		printf("%.3lf\n",dfs(f));
	}
	return 0;
} 

递推方式:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=20000+10;
int n,f,c[MAX];
double dp[MAX],p;//dp[i]表示战斗力为i时出去的期望

LL cal(){
	LL sum=0;
	for(int i=1;i<=n;++i)sum+=(int)(p*c[i]*c[i]);
	return sum;
}

int main(){
	p=(1.0+sqrt(5))/2.0;
	while(~scanf("%d%d",&n,&f)){
		int maxc=0;
		for(int i=1;i<=n;++i){scanf("%d",&c[i]);maxc=max(c[i],maxc);}
		LL sum=cal();
		for(int i=maxc+1;i<maxc+maxc+1;++i)dp[i]=sum*1.0/n;
		for(int i=maxc;i>=f;--i){
			dp[i]=0;
			for(int j=1;j<=n;++j){
				if(i>c[j]){
					dp[i]+=int(p*c[j]*c[j])*1.0/n;
				}else{
					dp[i]+=(dp[i+c[j]]+1)/n;
				}
			}
		}
		printf("%.3lf\n",dp[f]);
	}
	return 0;
}