uva11722 Joining with Friend

来源:互联网 发布:windows虚拟机软件 编辑:程序博客网 时间:2024/05/19 02:20

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 xed. In fact your train can reach in
any time within the interval [ t 1 ;t 2 ] with equal probability. The
other one will reach in any time within the interval [ s 1 ;s 2] 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 rst line of input will denote
the number of cases T ( T< 500). Each of the following T line will
contain 5 integers t 1 , t 2 , s 1 , s 2 , w (360  t 1 < t 2 < 1080,
360  s 1 < s 2 < 1080 and 1  w  90). All inputs t 1 , t 2 , s 1 , s
2 and w are given in minutes and t 1 , t 2 , s 1 , s 2 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 1 e

把两辆火车到达时间分别看成xy,所求概率就是直线y=x+wy=xw中间区域与矩形(s1,s2,t1,t2)相交面积占矩形总面积的比例。
具体实现上,分类讨论的情况比较多,可以分别计算两条直线以下的矩形面积再相减。

#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;int t1,t2,s1,s2,w;double calc(int x){    if (t1+x>=s2) return (s2-s1)*(t2-t1);    if (t2+x<=s1) return 0;    if (t1+x>=s1&&t2+x>=s2) return (t2-t1)*(s2-s1)-(double)(s2-t1-x)*(s2-t1-x)/2;    if (t1+x<=s1&&t2+x<=s2) return (double)(t2-s1+x)*(t2-s1+x)/2;    if (t1+x<=s1&&t2+x>=s2) return (double)(2*t2-s1-s2+2*x)*(s2-s1)/2;    return (double)(t1+t2-2*s1+2*x)*(t2-t1)/2;}int main(){    int T,K;    scanf("%d",&T);    for (K=1;K<=T;K++)    {        scanf("%d%d%d%d%d",&t1,&t2,&s1,&s2,&w);        printf("Case #%d: %.8f\n",K,(calc(w)-calc(-w))/(t2-t1)/(s2-s1));    }}
0 0
原创粉丝点击