11722-joining with Friend
Going from Dhaka to Chittagong by train and you came to know one of the your old friends is going
From city Chittagong to Sylhet. You also know that both the trains would 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 is not fixed. In fact your train can
Reach in any time within the interval [T1, T2] with equal probability. The other one would reach in any
Time within the interval [S1, S2] with equal probability. Each of the trains would stop for W minutes after
Reaching the junction. You can only see your friend, if in some time both of the trains are present in the
Station. Find the probability that's can see your friend.
Input
The first line of input would denote the number of cases T (T < 500). Each of the following T-line would
Contain 5 integers t1, t2, S1, S2, W (360≤t1 < T2 < 1080, 360≤s1 < S2 < and 1≤w≤90). All
Inputs T1, T2, S1, S2 and W is given in minutes and T1, T2, S1, S2 is 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. 1e−6 error in your output would 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
Puzzle: Test instructions is two people meet, arrival time in a time period, and then stay w minutes then go, let the probability of meeting;
Two set two person arrival time to X, Y, two people meet for-w=<x-y<=w;
In a two-dimensional coordinate system, the total sample is (T2-T1) (S2-S1); only the area of the parallel-w=<x-y<=w with the rectangle is required to intersect the area of the rectangle;
Code:
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include < algorithm>using namespace std; #define MEM (x, y) memset (x,y,sizeof (×)) Double t1,t2,s1,s2,w;double GETMJ (double b) { Double Y1=t1+b,y2=t2+b,x1=s1-b,x2=s2-b;if (Y1>=S2) return 0;if (X1>=T2) return (S2-S1) * (T2-T1);/*if (Y1>=S1) &&Y2>=S2) return 0.5* (s2-y1) * (T2-X2-T1), if (Y1<S1&&Y2>=S2) return 0.5* (S2-S1) * (X2-X1) + ( X1-T1) * (S2-S1), if (Y1>S1&&Y2<S2) return 0.5* (T2-T1) * (y2-y1) + (T2-T1) * (S2-y2), if (X1>=T2) return ( S2-S1) * (T2-T1), if (Y1<=S1&&Y2<=S2) return (S2-S1) * (T2-T1) -0.5* (t2-x1) * (Y2-S1), */if (Y1>=S1) {if (x2 <=T2) return 0.5* (X2-T1) * (s2-y1), Else return 0.5* (T2-T1) * (y2-y1) + (s2-y2) * (T2-T1);} Else{if (X2<=T2) return 0.5* (S2-S1) * (x2-x1) + (X1-T1) * (S2-S1), else return (S2-S1) * (T2-T1) -0.5* (t2-x1) * (Y2-S1);//Pit, Here is wrong, wrong half a day, actually angle 45 degrees, write a line}}int main () {int t,kase=0;scanf ("%d", &t), while (t--) {scanf ("%lf%lf%lf%lf%lf", &t1, &T2,&S1,&S2,&W);Double ans= (GETMJ (-W)-GETMJ (W))/((S2-S1) * (T2-T1));p rintf ("Case #%d:%.8lf\n", ++kase,ans);} return 0;}
Uva11722-joining with Friend (geometric probability)