UVa 11722 - Joining with Friend (概率 数形结合)

解决UVa 11722问题,分析两列火车在指定站会面的概率。通过数形结合的方法,将问题转化为求解线段与直线y = x + w 和 y = x - w相交的面积,从而确定会面的概率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

I I U P C   2 0 0 9

 

Problem J: 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 t1t2s1s2 and w are given in minutes and t1, t2, s1, s2 are minutes since midnight00:00.

 

Output

For each test case print one line of output in the format “Case #k: p” Here k is the case number andp is the probability of seeing your friend. Up to 1e-6 error in your output will be acceptable.

 

Sample Input

Output for Sample Input

2

1000 1040 1000 1040 20

720 750 730 760 16

Case #1: 0.75000000

Case #2: 0.67111111

 

Problem Setter: Md. Towhidul Islam Talukder

Special Thanks: Samee Zahur, Mahbubul Hasan

 



题意:

你乘火车A 你的小伙伴乘火车B,你们想要在X站会面

火车A到X站的时间可能是[t1, t2]中的某时刻(等概率),火车B到X站的时间可能是[s1, s2]中的某时刻(等概率)。

AB车到站后都会停留W分钟。只有在同一时刻AB车都停在X站的时候,你才可以和你小伙伴会面。

求你们能够会面的概率


大白上的思路很巧妙 把问题转化为求面积

x轴表示A车到站的时刻 即[t1, t2]是一条平行于x轴的线段L1

y轴表示B车到站的时刻 即[s1, s2]是一条平行于y轴的线段L2

则L1 L2就是一个矩形的两条相邻的边,整个概率空间就是这个矩形

则会面的概率就是 矩形与(直线y = x + w 与 y = x - w之间的面积)的相交部分



#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const double PI = (4.0*atan(1.0));

double t1, t2, s1, s2, w;
double all_s;

double area(double d) {
    if(t1 + d > s2-eps) return all_s;
    if(t2 + d < s1+eps) return 0;
    double ret = 0;
    double x1 = s1 - d, x2 = t2 + d;
    double x3 = t1 + d, x4 = s2 - d;
    if(t1-eps < x1 && x1 < t2 + eps) {
        if(s1-eps < x2 && x2 < s2 + eps) {
            ret = (t2-x1) * (x2-s1) * 0.5;
        } else {
            ret = (t2-x1 + t2-x4) * (s2-s1) * 0.5;
        }
    } else {
        if(s1-eps < x2 && x2 < s2 + eps) {
            ret = (x3-s1 + x2-s1) * (t2-t1) * 0.5;
        } else {
            ret = all_s - (s2-x3)*(x4-t1) * 0.5;
        }
    }
    return ret;
}

int main() {
    int T;

    scanf("%d", &T);
    for(int kase=1; kase<=T; kase++) {
        scanf("%lf%lf%lf%lf%lf", &t1, &t2, &s1, &s2, &w);
        all_s = (t2 - t1) * (s2 - s1);
        double s1 = area(w);
        double s2 = area(-1*w);
        double ans = (s1 - s2) / all_s;
        printf("Case #%d: %.8lf\n", kase, ans);
    }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值