UVA - 11722 Joining with Friend (概率)


You are going from Dhaka to Chittagong by train and you came to know one of your old friends is going from city Chittagong to Sylhet. You also know that both the trains will have a stoppage at junction Akhaura at almost same time. You wanted to see your
friend there. But the system of the country is not that good. The times of reaching to Akhaura for both trains are not fixed. In fact your train can reach in any time within the interval
[t1, t2] with equal probability. The other one will reach in any time within the interval
[s1, s2] with equal probability. Each of the trains will stop for w minutes after reaching the junction.  You can only see your friend, if in some time both of the trains is present in the station. Find the probability that you can see your
friend.


Input


The first line of input will denote the number of cases T (T < 500). Each of the following
T line will contain 5 integers t1, t2, s1, s2, w (360 ≤ t1 < t2 < 1080, 360 ≤ s1 < s2 < 1080 and 1 ≤ w ≤ 90). All inputs
t1, t2, s1, s2 and
w are given in minutes and t1, t2, s1, s2 are minutes since midnight
00:00.


Output


For each test case print one line of output in the format “Case #k: p” Here
k is the case number and p is the probability of seeing your friend. Up to
1e-6 error in your output will be acceptable.


Sample Input


Output for Sample Input


2

1000 1040 1000 1040 20

720 750 730 760 16


Case #1: 0.75000000

Case #2: 0.67111111


Problem Setter: Md. Towhidul Islam Talukder

Special Thanks: Samee Zahur, Mahbubul Hasan

题意:你和朋友都要乘坐火车,为了在A城市见面,你会在时间区间[t1, t2]中的任意时刻以相同的概率密度到达,你的朋友会在时间区间[s1, s2]内的任意时刻以相同的概率密度到达,你们的火车都会在车站停留W秒,只有在同一时刻火车都在城市A的时候,才会相见,问你这件事情的概率

思路:将密度构造成矩形,然后求解y = x +(-) b围成的图形占矩阵面积的大小,分情况讨论

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

double t1, t2, s1, s2, w;

double cal(double b) {
	double sum = (t2-t1) * (s2-s1);
	double x1 = t1, y1 = t1 + b;
	double x2 = t2, y2 = t2 + b;
	if (y2 <= s1)
		return 0;
	if (y1 <= s1) {
		if (y2 <= s2)
			return 0.5 * (y2 - s1) * (t2 - (s1 - b));
		else return 0.5 * (t2 - s1 + b + t2 - s2 + b) * (s2 - s1);
	}
	else if (y1 < s2) {
		if (y2 <= s2)
			return 0.5 * (t1 + b - s1 + t2 + b - s1) * (t2 - t1);
		else return sum - 0.5 * (s2 - t1 - b) * (s2 - b - t1);
	}
	else return sum;
}

int main() {
	int t, cas = 1;
	scanf("%d", &t);
	while (t--) {
		scanf("%lf%lf%lf%lf%lf", &t1, &t2, &s1, &s2, &w);
		double ans = cal(w) - cal(-w);
		ans /= (t2-t1) * (s2-s1);
		printf("Case #%d: %.8lf\n", cas++, ans);
	}
	return 0;
}
时间: 2024-10-27 12:36:50

UVA - 11722 Joining with Friend (概率)的相关文章

uva 11722 - Joining with Friend(概率)

题目连接:uva 11722 - Joining with Friend 题目大意:你和朋友乘火车,而且都会路过A市.给定两人可能到达A市的时段,火车会停w.问说两人能够见面的概率. 解题思路:y = x + w 和y = x - w在给定时间内围成的面积除以时间的总面积,就是求面积的时候要分情况处理. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int t

UVA - 11722 Joining with Friend 几何概率

                        Joining with Friend You are going from Dhaka to Chittagong by train and you came to know one of your old friends is goingfrom city Chittagong to Sylhet. You also know that both the trains will have a stoppage at junctionAkhaur

uva 11427 - Expect the Expected(概率)

题目链接:uva 11427 - Expect the Expected 题目大意:你每天晚上都会玩纸牌,每天固定最多玩n盘,每盘胜利的概率为p,你是一个固执的人,每天一定要保证胜局的比例大于p才会结束游戏,若n局后仍没有,就会不开心,然后以后再也不完牌,问说你最多会玩多少个晚上. 解题思路:当j/i ≤ p时有dp(i-1,j) (1-p) + dp(i-1, j-1) p,其他dp(i,j) = 0.Q=∑d(n,i) 列出数学期望公式: EX=Q+2Q(1?Q)+3Q(1?Q)2+- s=

uva 11637 - Garbage Remembering Exam(概率)

题目链接:uva 11637 - Garbage Remembering Exam 题目大意:大白数里有很详细的题意. 解题思路:对于有序的序列来说,考虑每个位置为有效性的概率.C(2?kn?1?x)?A(2k2k)?A(n?1?xn?1?x)A(n?1n?1) x为考虑当前位置,然后与该位置距离小于等于k的位置个数.该位置有效的话,则对应的要将原先邻近的2*k个单词放到另外的位置上. #include <cstdio> #include <cstring> #include &l

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

UVA 10529 - Dumb Bones 题目链接 题意:你试图把一些多米诺骨牌排成直线,然后推倒它们.但是如果你在放骨牌的时候不小心把刚放的骨牌碰倒了,它就会把相临的一串骨牌全都碰倒,而你的工作也被部分的破坏了. 比如你已经把骨牌摆成了DD__DxDDD_D的形状,而想要在x这个位置再放一块骨牌.它可能会把左边的一块骨牌或右边的三块骨牌碰倒,而你将不得不重新摆放这些骨牌. 这种失误是无法避免的,但是你可以应用一种特殊的放骨牌方法来使骨牌更多的向一个方向倒下. 给出你要摆放的骨牌数目,以及放

uva 1636 - Headshot(简单概率问题)

直接扣一枪没有子弹 是条件概率 转一下再扣一枪 是简单事件发生的概率 前者用00的个数除以00和01子串的总数 后者用0的个数除以所有数字的个数 然后换算一下运算方式比较即可 #include<cstdio> #include<cstring> const int maxn = 105; char s[105]; int cnt0,cnt1,cnt2,cnt3; int main() { while(scanf("%s",s+1)!=EOF) { cnt0=0;

UVA 11762 - Race to 1(概率)

UVA 11762 - Race to 1 题意:给定一个n,每次随即选择一个n以内的质数,如果不是质因子,就保持不变,如果是的话,就把n除掉该因子,问n变成1的次数的期望值 思路:tot为总的质数,cnt为质因子个数,那么f(n)=(1?cnt/tot)?f(n)+∑f(n/prime)?(1/tot),然后利用记忆化搜索去做即可 代码: #include <stdio.h> #include <string.h> const int N = 1000005; int t, n,

UVA 11427 - Expect the Expected(概率递推期望)

UVA 11427 - Expect the Expected 题目链接 题意:玩一个游戏,赢的概率p,一个晚上能玩n盘,如果n盘都没赢到总赢的盘数比例大于等于p,以后都不再玩了,如果有到p就结束 思路:递推,dp[i][j]表示玩i盘,赢j盘的概率,那么一个晚上玩了n盘小于p的概率递推式为: dp(i,j)=dp(i?1,j)?(1?p)+dp(i?1,j?1)?p 总和为Q=dp(n,0)+dp(n,1)+...+dp(n,x)(x/n<p) 那么每个晚上失败的概率Q就求出来了,那么平均玩的

UVA 10828 - Back to Kernighan-Ritchie(概率+高斯消元)

UVA 10828 - Back to Kernighan-Ritchie 题目链接 题意:给图一个流程图,有结点的流程,每次进入下一个流程概率是均等的,有q次询问,求出每次询问结点的执行期望 思路:高斯消元,每个结点的期望等于所有前趋结点的期望/出度的和,由于存在无限循环的情况,不能直接递推,利用高斯消元去做,判断无解的情况既为无限循环,注意如果一个式自xi为0,但是xn也为0,xi值应该是0,表示无法到达 代码: #include <cstdio> #include <cstring