UVA - 1498 Activation (DP+概率)

Description

After 4 years‘ waiting, the game "Chinese Paladin 5" finally comes out. Tomato is a crazy fan, and luckily he got the first release. Now he is at home, ready to begin his journey. But before starting the game, he must first activate the product on the official
site. There aretoo many passionate fans that the activation server cannot deal with all the requests at the same time, so all the players must wait in queue. Each time, the server deals with the request of the first player in the queue, and the result may
be one of the following, each has aprobability:

  1. Activation failed: This happens with the probability of p1. The queue remains unchanged and the server will try to deal with the same request the next time.
  2. Connection failed: This happens with the probability of p2. Something just happenedand the first player in queue lost his connection with the server. The server will then remove his request from the queue. After that, the player
    will immediately connect to the server againand starts queuing at the tail of the queue.
  3. Activation succeeded: This happens with the probability of p3. Congratulations, theplayer will leave the queue and enjoy the game himself.
  4. Service unavailable: This happens with the probability of p4. Something just happened and the server is down. The website must shutdown the server at once. All the requests that are still in the queue will never be dealt.

Tomato thinks it sucks if the server is down while he is still waiting in the queue and there are no more than
K - 1 guys before him. And he wants to know the probability that this ugly thing happens.

To make it clear, we say three things may happen to Tomato: he succeeded activating the game; the server is down while he is in the queue and there are no more than
K - 1 guys before him; the server is down while he is in the queue and there are at least
K guys before him.Now you are to calculate the probability of the second thing.

Input

There are no more than 40 test cases. Each case in one line, contains three integers and four real numbers:
N, M (1MN2000),
K (K1),
p1, p2, p3, p4(0p1,
p2, p3, p41,
p1 + p2 + p3 + p4 = 1), indicating there are
N guys in the queue (the positions are numbered from 1 to
N), and at the beginning Tomato is at the
Mth position, with the probability p1,
p2, p3, p4 mentioned above.

Output

A real number in one line for each case, the probability that the ugly thing happens.

The answer should be rounded to 5 digits after the decimal point.

Sample Input

2 2 1 0.1 0.2 0.3 0.4
3 2 1 0.4 0.3 0.2 0.1
4 2 3 0.16 0.16 0.16 0.52

Sample Output

0.30427
0.23280
0.90343
题意::有n个人排队等着在官网上激活游戏。Tomato排在第m个。对于队列中的第一个人。有一下情况:
1、激活失败,留在队列中等待下一次激活(概率为p1)
2、失去连接,出队列,然后排在队列的最后(概率为p2)
3、激活成功,离开队列(概率为p3)
4、服务器瘫痪,服务器停止激活,所有人都无法激活了。
求服务器瘫痪时Tomato在队列中的位置<=k的概率
思路:kuangbinGG写的很好,我就不说了
借鉴kuangbinGG的题解:
概率DP;
设dp[i][j]表示i个人排队,Tomato排在第j个位置,达到目标状态的概率(j<=i)
dp[n][m]就是所求
j==1:    dp[i][1]=p1*dp[i][1]+p2*dp[i][i]+p4;
2<=j<=k: dp[i][j]=p1*dp[i][j]+p2*dp[i][j-1]+p3*dp[i-1][j-1]+p4;
k<j<=i:  dp[i][j]=p1*dp[i][j]+p2*dp[i][j-1]+p3*dp[i-1][j-1];
化简:
j==1:    dp[i][1]=p*dp[i][i]+p41;
2<=j<=k: dp[i][j]=p*dp[i][j-1]+p31*dp[i-1][j-1]+p41;
k<j<=i:  dp[i][j]=p*dp[i][j-1]+p31*dp[i-1][j-1];

其中:
p=p2/(1-p1);
p31=p3/(1-p1)
p41=p4/(1-p1)

可以循环i=1->n 递推求解dp[i].在求解dp[i]的时候dp[i-1]就相当于常数了。
在求解dp[i][1~i]时等到下列i个方程
j==1:   dp[i][1]=p*dp[i][i]+c[1];
2<=j<=k:dp[i][j]=p*dp[i][j-1]+c[j];
k<j=i:  dp[i][j]=p*dp[i][j]+c[j];
其中c[j]都是常数了。上述方程可以解出dp[i]了。
首先是迭代得到 dp[i][i].然后再代入就可以得到所有的dp[i]了。

注意特判一种情况。就是p4<eps时候,就不会崩溃了,应该直接输出0

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 2020;
const double eps = 1e-5;

double c[maxn], f[maxn], dp[maxn][maxn];

int main() {
	int n, m, k;
	double p1, p2, p3, p4;
	while (scanf("%d%d%d%lf%lf%lf%lf", &n, &m, &k, &p1, &p2, &p3, &p4) != EOF) {
		if (p4 < eps) {
			printf("0.00000\n");
			continue;
		}
		double p = p2/(1-p1);
		double p41 = p4/(1-p1);
		double p31 = p3/(1-p1);
		f[0] = 1.0;
		for (int i = 1; i <= n; i++)
			f[i] = p * f[i-1];

		dp[1][1] = p41 / (1-p);
		c[1] = p41;
		for (int i = 2; i <= n; i++) {
			for (int j = 2; j <= k; j++) c[j] = p31 * dp[i-1][j-1] + p41;
			for (int j = k+1; j <= i; j++) c[j] = p31 * dp[i-1][j-1];

			double tmp = c[1] * f[i-1];
			for (int j = 2; j <= i; j++)
				tmp += c[j] * f[i - j];

			dp[i][i] = tmp / (1 - f[i]);
			dp[i][1] = p * dp[i][i] + c[1];

			for (int j = 2; j < i; j++)
				dp[i][j] = p * dp[i][j-1] + c[j];
		}

		printf("%.5lf\n", dp[n][m]);
	}
	return 0;
}
时间: 2024-10-11 11:33:55

UVA - 1498 Activation (DP+概率)的相关文章

HDU 4089 &amp;&amp; UVa 1498 Activation 带环的概率DP

要在HDU上交的话,要用滚动数组优化一下空间. 这道题想了很久,也算是想明白了,就好好写一下吧. P1:激活游戏失败,再次尝试. P2:连接失服务器败,从队首排到队尾. P3:激活游戏成功,队首的人出队. P4:服务器down掉,所有人都不能激活了. 设d(i, j)表示i个人排队,主人公排在第j位,发生所求事件的概率. d(i, 1) = P1 d(i, 1) + P2 d(i, i) + P4 //分别对应激活失败,重新尝试:连接失败排到队尾:服务器down掉 特殊地可以直接计算出 d(1,

uva 10529 - Dumb Bones(概率+区间dp)

题目连接:uva 10529 - Dumb Bones 题目大意:给定n,表示要放n个骨牌,每次放下骨牌,有可能向左倒的概率为pl,向右倒的概率为pr,如果倒下,会将那一侧的骨牌全部推倒,可以选择位置先后放骨牌,问说一种放骨牌次数最少的期望是多少. 解题思路:dp[i]表示放i个骨牌需要的步数期望,维护一个最优放的位置,dp[i] = min\{ (从i-1到i的步数)} + (0到i-1的步数)} (从i-1到i的步数):dp[i?j?1]?pl+dp[j]?pr+11?pl?pr (0到i-

UVA 11427 Expect the Expected(DP+概率)

链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35396 [思路] DP+概率 见白书. [代码] 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 5 const int N = 100+10; 6 7 int n,a,b; 8 double f[N][N]; 9 10 int main() { 11 int T,kase=

UVA 12230 - Crossing Rivers(概率)

UVA 12230 - Crossing Rivers 题目链接 题意:给定几条河,每条河上有来回开的船,某一天出门,船位置随机,现在要求从A到B,所需要的期望时间 思路:每条河的期望,最坏就是船刚开走3L/V,最好就是直接上船L/V,期望为4L/V/2 = 2L/V,然后在算上陆地上的时间,就是答案 代码: #include <stdio.h> #include <string.h> int n; double d, p, l, v; int main() { int cas =

uva 11133 - Eigensequence(dp)

题目链接:uva 11133 - Eigensequence 题目大意:给定一个序列a,能够确定一个序列b,要求: 1)b[1]=a[1] 2)a[j?1]<b[j]≤a[j]且b[j]a[j]?a[j?1]为整数,j>1的时候 假设对于每一个ai=bi,则称b序列为Eigensequence序列. 如今给定a1和an,问有多少个Eigensequence序列. 解题思路:dp[i][j]表示第i个数为j的情况有多少种,假设k整除k-j,dp[i+1][k]+=dp[i][j]. #inclu

uva 11367 dijkstra+dp状态压缩

题意:给出n个地点 和 每个地点的油价 ,有 m 条边 , 并给出每条边长度 .1单位汽油可以走1千米  , 油箱的容量为 c , 在初始点 s 时 , 油箱中的油为 0 , 求s 到 t 的最小花费 . 解法: 定义 状态 d[i][j] 表示到达 地点 i 且油箱中有 j 单位油时的最小 花费. 对于状态的转移时 , 有两种方法: 1.把每个点的所有状态都求出 2.不把每个点的状态都求出 , 而是一单位一单位的加油. 对于第一种方法 , 会超时 , 因为每个点的状态太多 , 但是能用的状态就

CF235 Let&#39;s Play Osu![dp+概率]

题意: 给n个位置,给出1-n上每个位置出现O的概率pi,记分规则如下,连续的x个O记为x^2分,求和,如 XXOOOXOXOOXX得分为 求得分的期望 思考一下,我们能比较容易地得出O(n^2)的方法 令dp[i]为前i的得分期望 那么 显然这题 考虑一下变换记分的方式 我们有 那么记分方式就变为 一段连续的O,有多少对O×2+O的个数 一对O可以贡献2分 现在得分来源变为两个地方 一对O(2分),和单个O(1分) 我们知道 期望=概率×收益 我们找到每个对O的概率×2 再找到单个O×出现概率

uva 10593 - Kites(dp)

题目链接:uva 10593 - Kites 题目大意:给出一个n*n的图,表示一张纸板,问有多少种方法做成风筝,风筝必须是正方形或者是菱形,并且不能有洞. 解题思路:分正方形和菱形两种情况讨论: 正方形,dp[i][j]表示以i,j为右下角的正方形 dp[i][j]=min(dp[i?1][j],dp[i][j?1]) 并且如果黄色部分也为'x'的话,dp[i][j]++ 菱形,dp[i][j]表示菱形的正下角 同样地市黄色部分如果为'x'的话,dp[i][j]++ #include <cst

uva 10237 - Bishops(dp)

克里斯·厄姆森 谷歌今天在 Code 大会上发布了新的无人驾驶汽车.该汽车看起来像是有轮子的缆车,它既没有驾驶盘,也没有刹车踏板和加速装置.Re/code 采访了谷歌无人驾驶汽车项目主管克里斯·厄姆森(Chris Urmson),期间谈及该项目革命背后的概念.产品何时上路等问题. 谷歌在过去的 5 年里改装了现成车型去试验无人驾驶技术.除了车顶的旋转激光装置外,它们看上去跟普通车没什么不同.而该公司今天发布的汽车看上去则非常怪异.它们又小又圆,配备各种小型黑色传感器(车顶也有旋转激光装置),用泡