UVa11722

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
2
1000 1040 1000 1040 20
720 750 730 760 16
Sample Output
Case #1: 0.75000000
Case #2: 0.67111111

题意:

两个人到达车站的时间分别在时间区间t1~t2, s1~s2内;并且他们到站后都会停留w分钟.问他们相遇的概率。

分析:

以坐标(t1,s1),(t2,s2)作边平行于坐标轴的矩阵,再作直线y=x+w和y=x-w,它们之间与矩阵相覆盖的阴影面积占矩阵总面积比就是答案。过4个矩阵顶点作平行于y=x的直线,共分5个区域。分别根据w讨论y=x+w和y=x-w在哪个区域内,再计算非阴影面积,之后总面积减非阴影面积再除以总面积就可得到答案。

 1 #include <stdio.h>
 2 #include <algorithm>
 3 using namespace std;
 4 int main(){
 5     int T,tt=0;
 6     scanf("%d",&T);
 7     while(T--){
 8         int a[10],t1,t2,s1,s2,w,i,j,k,p,x,y;
 9         double s = 0,sum; //s记录非阴影面积
10         scanf("%d%d%d%d%d",&t1,&t2,&s1,&s2,&w);
11         a[0] = s1 - t2,a[1] = s2 - t2;
12         a[2] = s1 - t1,a[3] = s2 - t1;
13         sort(a,a + 4);
14         sum = (t2 - t1) * (s2 - s1);
15         p = min(s2 - s1,t2 - t1); // 短边
16         if(-w <= a[0]) s += 0;
17         else if(-w <= a[1])
18             x = t2 - s1 - w, // 右点横坐标减去交点横坐标的值
19             y = t2 - s1 - w,s += 0.5 * x * y; // 45度三角形
20         else if(-w <= a[2])
21             s += 0.5 * p * p,x = -w - a[1],s += x * p;
22         else if(-w <= a[3])
23             x = s2 + w - t1,y = s2 + w - t1,
24             s += (t2 - t1) * (s2 - s1) - 0.5 * x * y;
25         else s += (t2 - t1) * (s2 - s1);
26         if(w >= a[3]) s += 0;
27         else if(w >= a[2])
28             y = x = s2 - w - t1,s += 0.5 * x * y;
29         else if(w >= a[1])
30             s += 0.5 * p * p,x = a[2] - w,s += x * p;
31         else if(w >= a[0])
32             y = x = t2 - s1 + w,s += (t2 - t1) * (s2 - s1) - 0.5 * x * y;
33         else s += (t2 - t1) * (s2 - s1);
34         printf("Case #%d: %.8lf\n",++tt,(sum - s) / sum);
35     }
36     return 0;
37 }

时间: 2024-08-05 03:13:09

UVa11722的相关文章

UVA11722 Jonining 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 going from city Chittagong to Sylhet. You also know that both the trains will have a stoppage at junction Akhaura at almost same time.

uva11722 - Joining with Friend(几何概率)

11722 - 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 junctionAkhaura at almost same

[uva11722&amp;&amp;cogs1488]和朋友会面Joining with Friend

几何概型,<训练指南>的题.分类讨论太神啦我不会,我只会萌萌哒的simpson强上~这里用正方形在y=x-w的左上方的面积减去在y=x+w左上方的面积就是两条直线之间的面积,然后切出来的每一小段肯定是梯形或三角形,所以可以写得和一般的Simpson有点区别. #include<cstdio> const double eps=1e-8; int a1,a2,b1,b2,w; double f1(double x){ if(x+w<=b1)return b2-b1; retur

UVA11722 Joining with Friend

题意:知道你和朋友的到达时间,两人最多等W范围分钟,问两人会面的概率 题解:概率空间是一个平面上的矩形,计算面积即可,这里用到了计算几何 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<algorithm> const int maxn = 555; const int maxisn = 10; const double eps =

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y

JSZX_HC_2016_R5

#1 ccz 200 #2 CTL 130 #3 KPM 130 本来以为准备挺充分的,开始后还是出现一些状况 >_< 好在还算顺利…… A AC人数:4   平均分:70 题目描述 给定一个自然数n,求出所有不大于n的自然数中含有49的数的个数. 对于每个测试数据,输入一个整数n,输出答案. 数据保证输入和答案都在long long范围内. Problem 数位DP,HDU3555 Solution B AC人数:1   平均分:31 在空间中存在两个质点A.B,它们在每个时间点上出现的位置