UV-11722 joining with friend (probability)

Source: Internet
Author: User

 

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 casesT (T & lt; 500). Each of the followingTLine will contain5IntegersT1, T2, S1, S2, w (360 ≤ t1 <t2 <1080,360 ≤ S1 <S2 <1080 and 1 ≤ W ≤ 90). All inputsT1,T2,S1,S2AndWAre given in minutes andT1, T2, S1, S2Are minutes since midnight00: 00.

 

Output

For each test case print one line of output in the format"Case # K: P"HereKIs the case number andPIs the probability of seeing your friend. Up1e-6Error 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

You and your friends are going to take a train. In order to meet each other in City A, you will arrive at any time point in the time range [T1, T2] with the same probability density, your friends will arrive at any time point in the time range [S1, S2] with the same probability density, and your train will stay at the station for W seconds, you can only see each other when the train is in City A at the same time.

Train of Thought: Construct the density into a rectangle, and then calculate the size of the matrix area of the image y = x + (-) B.

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;double t1, t2, s1, s2, w;double cal(double b) {double sum = (t2-t1) * (s2-s1);double x1 = t1, y1 = t1 + b;double x2 = t2, y2 = t2 + b;if (y2 <= s1)return 0;if (y1 <= s1) {if (y2 <= s2) return 0.5 * (y2 - s1) * (t2 - (s1 - b));else return 0.5 * (t2 - s1 + b + t2 - s2 + b) * (s2 - s1);}else if (y1 < s2) {if (y2 <= s2) return 0.5 * (t1 + b - s1 + t2 + b - s1) * (t2 - t1);else return sum - 0.5 * (s2 - t1 - b) * (s2 - b - t1);}else return sum;}int main() {int t, cas = 1;scanf("%d", &t);while (t--) {scanf("%lf%lf%lf%lf%lf", &t1, &t2, &s1, &s2, &w);double ans = cal(w) - cal(-w);ans /= (t2-t1) * (s2-s1);printf("Case #%d: %.8lf\n", cas++, ans);}return 0;}






UV-11722 joining with friend (probability)

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.