【ZOJ】3604 Help Me Escape(概率DP)

题目大意:有n条路可以选择,随机选择,选择了这条路时有一条规则,假如攻击力f大于了这条路的ci,那么可以从这条路逃出去,花费ti(有对应公式计算)

假如小于等于该值,则花费一天,并且攻击力增加ci,重复刚才的操作。问最终的期望是多少。

思路:

dp[i]表示的是攻击力为i的情况下,出去的期望。

根据期望的概念可以得

状态方程:dp[i]+=(1+dp[i+c[i])/n (当攻击力小于等于c[i])

dp[i]+=t[i]/n                  (当攻击力大于c[i])

可以用一般递推去做,也可以用记忆化搜索,AC代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int p[10005];
double dp[20005];
int c[110];
int n,f;
/*
记忆搜索
*/
double gao(int k)
{
	double &ans = dp[k];
	if (ans != 0)return ans;
	ans = 0;
	for (int i = 1; i <= n;i++)
	if (k > c[i])
		ans += p[i] / (1.0*n);
	else
		ans += (1 + gao(k + c[i])) / (1.0*n);
	return ans;
}
int main()
{
	while (cin >> n >> f)
	{
		double mid = (1.0 + sqrt(5.0)) / 2;
		for (int i = 1; i <= n; i++)
			scanf("%d", &c[i]);
		memset(dp, 0, sizeof(dp));
		sort(c + 1, c + 1 + n);
		for (int i = 1; i <= n; i++)
			p[i] = c[i] * c[i] * mid;
		gao(f);
		/*
		一般递推方法如下:
		*/
		/*int mid2 = c[n] + 1;
		dp[mid2] = 0;
		for (int i = 1; i <= n; i++)
			dp[mid2] += p[i];
		dp[mid2] /= n;

		if (f > c[n])
		{
			printf("%.3lf\n", dp[mid2]);
			continue;
		}*/
		/*int k = c[n] << 1;
		for (int i = c[n] + 1; i <= k; i++)
			dp[i] = dp[mid2];
		for (int i = c[n]; i >= f; i--)
		for (int j = 1; j <= n; j++)
		if (i <= c[j])
			dp[i] += (1.0 + dp[i + c[j]]) / n;
		else
			dp[i] += p[j] / (1.0*n);*/
		printf("%.3lf\n", dp[f]);
	}
}
时间: 2024-10-04 13:25:49

【ZOJ】3604 Help Me Escape(概率DP)的相关文章

ZOJ 3640 Help Me Escape 概率dp

有一个吸血鬼被困了,有n条路可以逃出去,每条路有一个难度c[],他初始的战斗力是f,对于第i条路,若f > c[i]他花t[i]天就能出去,否则,他就停留一天,同时战斗力增加c[i]然后再选一条路走出去,他走每条路的概率是相同的.问他逃出去的天数的期望. 设dp[i]表示在战斗力为i时逃出去的期望值,那么可推出状态方程 dp[i] = 1/n * t[j](c[j] > i),dp[i] = 1/n * (1+dp[ i+c[j] ] )( c[j] <= i). 需要注意的是终态的确定

zoj 3640 Help Me Escape (概率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.  

[ACM] ZOJ 3329 One Person Game (概率DP,有环,巧妙转化)

One Person Game Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and Die3. Die1 has K1 faces. Die2 has K2 faces. Die3 has K3 faces. All the

zoj 3329 One Person Game 概率dp

先吐槽几句真心给数学跪了 题意: 有三个均匀的骰子,分别有k1,k2,k3个面,初始分数是0, 当掷三个骰子的点数分别为a,b,c的时候,分数清零,否则分数加上三个骰子的点数和, 当分数>n的时候结束.求需要掷骰子的次数的期望. 题解: 设 E[i]表示现在分数为i,到结束游戏所要掷骰子的次数的期望值. 显然 E[>n] = 0; E[0]即为所求答案; E[i] = ∑Pk*E[i+k] + P0*E[0] + 1; (Pk表示点数和为k的概率,P0表示分数清零的概率) 由上式发现每个 E[

ZOJ 3329 One Person Game 概率DP 好题

One Person Game Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and Die3. Die1 has K1 faces. Die2 has K2 faces. Die3 has K3faces. All the d

ZOJ 3329-One Person Game(概率dp,迭代处理环)

题意: 三个色子有k1,2,k3个面每面标号(1-k1,1-k2,1-k3),一次抛三个色子,得正面向上的三个编号,若这三个标号和给定的三个编号a1,b1,c1对应则总和置零,否则总和加上三个色子标号和,直到总和不小于n时结束,求抛色子的期望次数. 分析: 该题状态好分析 dp[i]表示和为i时的期望次数,dp[0]是答案 dp[i]=sum(dp[i+tmp]*p[tmp])+dp[0]*p0+1(tmp是三个色子可得到的标号和); 第一次看到这样的方程不怎么解,看了题解才知道用迭代法,每个d

ZOJ 3329 One Person Game 概率DP 期望 难度:2

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 本题分数为0的概率不确定,所以不能从0这端出发. 设E[i]为到达成功所需的步数,明显i>n时E[i]=0,当0<i<=n时E[i]=sigma(E[i+k]*pk)+E[0]*p0,(k是可以投出的除了恰为a,b,c以外的骰子之和), 在这个公式里,E[i]和E[0]都是未知的,设E[0]=x,则 E[i]=sigma(E[i+k]*pk)+x*p0+1, 因

zoj 3640 概率dp

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4808 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 Cai

ZOJ 3551 Bloodsucker (概率DP)

ZOJ Problem Set - 3551 Bloodsucker Time Limit: 2 Seconds      Memory Limit: 65536 KB In 0th day, there are n-1 people and 1 bloodsucker. Every day, two and only two of them meet. Nothing will happen if they are of the same species, that is, a people

ZOJ3640 Help Me Escape(概率dp)

p 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