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
题解:题意就是两个人见面,到达时间都在一个时间段内,然后停留w分钟再走,让求见面的概率;
两个把两个人到达时间设为x,y则两个人见面为 -w=<x-y<=w;
在二维坐标系上画出来,总样本是(t2-t1)(s2-s1);只需要求平行线-w=<x-y<=w与矩形相交的面积比矩形的面积即可;
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
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); //坑,这里写错了,错了半天,其实角度45度,写一个就行
}
}
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));
printf("Case #%d: %.8lf\n",++kase,ans);
}
return 0;
}