UVA 11722 Joining with Friend

来源:互联网 发布:mac pocomaker 编辑:程序博客网 时间:2024/06/08 10:11

题目链接

题意:  两个人坐火车, 在某个城市到站的时间段分别为[t1, t2] , [s1, s2],停在站台的时间均为w,问两人能见面的概率。

分析:高中的线性规划问题。总可能为S=(s2-s1)*(t2-t1);又停站时间为w所以|x-y|<=w,所以范围为t1<=x<=t2,s1<=y<=s2,|x-y|<=w所围成的面积。除一下即可。

#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespace std;double t1,t2,s1,s2,w;double tot;double get_area(double b){    if(t1+b>=s2)    return tot;    else if(t1+b<s1)    {        if(t2+b<=s1)        return 0;        else if(t2+b>s1&&t2+b<=s2)        return (t2+b-s1)*(t2+b-s1)*0.5;        else        return 0.5*(t2-s1+b+t2-s2+b)*(s2-s1);    }    else    {        if(t2+b>s2)        return tot-0.5*(s2-t1-b)*(s2-t1-b);        else        return 0.5*(t2-t1)*(t1+b-s1+t2+b-s1);    }}int main(void){    int cas=1;    int t;    scanf("%d",&t);    while(t--)    {        scanf("%lf%lf%lf%lf%lf",&t1,&t2,&s1,&s2,&w);        tot=(t2-t1)*(s2-s1);        double area1=get_area(w);        double area2=get_area(-w);        double area=area1-area2;        printf("Case #%d: %.8lf\n",cas++,area/tot);    }    return 0;}
0 0
原创粉丝点击