11722-joining with friend (probability)

Source: Internet
Author: User

Question connection: Ultraviolet A 11722-joining with friend

You and your friends take the train and all pass by city a. Given the time when the two may arrive at City A, the train will stop at W and ask about the probability of two people meeting each other.

Solution: divide the area enclosed by X + W and Y = x-W in a given time by the total area of the time, which means that the area should be divided into different situations.

#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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.