相遇~~~~

  题意:

     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.6711111

 

  思路分析:

            需要用到线性规划的知识,我们知道两人能相遇的条件是|y-x|<=w;

            分别讨论直线在的位置就好了

 

    

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 double t1, t2, s1, s2, w;
 5 double compute(double d)
 6 {
 7     double s = (t2 - t1)*(s2 - s1);
 8     if (t2 + d<s1)                     //1
 9         return 0;
10     if (t1 + d <= s1)                     //2      
11     {
12         if (t2 + d <= s2)
13             return (t2 - (s1 - d))*(t2 + d - s1)*0.5;
14         else
15             return ((t2 - (s2 - d)) + (t2 - (s1 - d)))*(s2 - s1)*0.5;
16 
17     }
18     if (t1 + d<s2)                      //3
19     {
20         if (t2 + d <= s2)
21             return ((t1 + d - s1) + (t2 + d - s1))*(t2 - t1)*0.5;
22         else
23             return s - ((s2 - t1 - d)*(s2 - d - t1))*0.5;
24     }
25 
26     else
27         return s;                        //4
28 }
29 int main()
30 {
31     int T, ans = 1;
32     cin >> T;
33     while (T--)
34     {
35         cin >> t1 >> t2 >> s1 >> s2 >> w;
36         //double a=(t2-t1)*(s2-s1); 
37         double k = compute(w) - compute(-w);   //有两条线
38         k /= (t2 - t1)*(s2 - s1);
39         printf("Case #%d: %.8lf\n", ans++, k);
40         //cout<<"Case #"<<ans++<<": "<<k/a<<endl;
41     }
42     return 0;
43 }

 

转载于:https://www.cnblogs.com/Lynn0814/p/4743487.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值