首页 > 代码库 > uva 11722 - Joining with Friend(概率)

uva 11722 - Joining with Friend(概率)

题目连接:uva 11722 - Joining with Friend

题目大意:你和朋友乘火车,并且都会路过A市,给定两人可能到达A市的时段,火车会停w,问说两人可以见面的概率。

解题思路:y = x + w 和y = x - w在给定时间内围成的面积除以时间的总面积,就是求面积的时候要分情况处理。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
int t1, t2, s1, s2, w;
double sum;

double solve (int k) {

    double ret = 0;

    if (s2 > t2 + k && s1 > t1 + k) {

        double r = max(t2 + k - s1, 0);
        ret = sum - r * r / 2; 
    } else if (s2 > t2 + k) {

        double r = 2 * s2 - t1 - t2 - 2 * k;
        ret = r * (t2 - t1) / 2;
    } else if (s1 > t1 + k) {

        double r = s2 + s1 - 2 * k - 2 * t1;
        ret = r * (s2 - s1) / 2;
    } else {

        double r = max(s2 - k - t1, 0);
        ret = r * r / 2;
    }

    if (k < 0)
        ret = sum - ret;
    return ret;
}

int main () {
    int cas;
    scanf("%d", &cas);
    for (int kcas = 1; kcas <= cas; kcas++) {
        scanf("%d%d%d%d%d", &t1, &t2, &s1, &s2, &w);

        sum = 1.0 * (t2 - t1) * (s2 - s1);
        double ans = solve(w) + solve(-w);
        printf("Case #%d: %.8lf\n", kcas, 1 - ans / sum);
    }
    return 0;
}