UVA11722概率问题之线性规划

来源:互联网 发布:蒙顶甘露 知乎 编辑:程序博客网 时间:2024/05/16 00:54

链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=2769&mosmsg=Submission+received+with+ID+13959080

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 followingT line will contain 5 integers t1, t2, s1, s2, w (360 ≤ t1 < t2 < 1080, 360 ≤ s1 < s2 < 1080 and 1 ≤ w ≤ 90). All inputst1, t2, s1, s2 andw 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” Herek is the case number and p is the probability of seeing your friend. Up to1e-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

简单二维线性规划问题:
输入:5 integers t1, t2, s1, s2, w (360 ≤ t1 < t2 < 1080, 360 ≤ s1 < s2 < 1080 and 1 ≤ w ≤ 90).
目标函数:0<=|s-t|<=w;计算目标的补集更简单
步骤:画出坐标轴,注意分类讨论;
分类讨论:(图形见书P141)
确定4个交点:
A(t1,t1+w),B(s2-w,s2),C(t2,t2-w),D(s1+w,s1)
aim1=0.5*(s2-ay)*(bx-t1),ay>=s1 && bx<=t2;
aim1=0.5*((s2-ay)+(s2-(t2+w))*(t2-t1), ay>=s1 && bx>t2;
aim1=0.5*(s1-w-t1+(bx-t1))*(s2-s1),ay<s1 && bx<=t2;
aim1=0;//否则
aim2=0.5*(s2-ay)*(bx-t1),cy<=s2 && dx>=t1;
aim2=0.5*((t1-w-s1)+(cy-s1))*(t2-t1), cy<=s2 && dx<t1;
aim2=0.5*(t2-(s2+w)+(t2-dx))*(s2-s1),cy>s2 && dx<=t1;
aim2=0;//否则
aim=((s2-s1)*(t2-t1)-aim1-aim2)/(s2-s1)*(t2-t1);
*/

#include <iostream>#include <cmath>#include <algorithm>#include <cstring>#include<cstdio>using namespace std;double t1,t2,s1,s2,w;double area(double b) // 求y=x+b下方在矩形中截取的面积{    double s=(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 s-0.5*(s2-t1-b)*(s2-t1-b);    }    else        return s;}int main(){    int t;    scanf("%d",&t);    for(int cas=1;cas<=t;cas++)    {        scanf("%lf%lf%lf%lf%lf",&t1,&t2,&s1,&s2,&w);        double ans=area(w)-area(-w);        ans/=(s2-s1)*(t2-t1);        printf("Case #%d: %.8lf\n",cas,ans);    }    return 0;}


0 0
原创粉丝点击