• UVa 11722 (概率 数形结合) Joining with Friend


    高中也做个这种类似的题目,概率空间是[t1, t2] × [s1, s2]的矩形,设x、y分别代表两辆列车到达的时间,则两人相遇的条件就是|x - y| <= w

    从图形上看就是矩形夹在两条平行线之间的部分。

    因为情况众多,一个一个分类很麻烦,而且可能有漏掉情况,所以就用计算几何的办法求了个凸多边形,多边形 与 矩形面积之比就是概率。

    代码有点挫,将就看,=_=||

     1 #include <cstdio>
     2 #include <vector>
     3 #include <cmath>
     4 using namespace std;
     5 
     6 struct Point
     7 {
     8     double x, y;
     9     Point(double x=0, double y=0):x(x), y(y) {}
    10 };
    11 typedef Point Vector;
    12 typedef vector<Point> Polygon;
    13 
    14 Point operator + (const Point& A, const Point& B)
    15 { return  Point(A.x+B.x, A.y+B.y); }
    16 
    17 Vector operator - (const Point& A, const Point& B)
    18 { return  Point(A.x-B.x, A.y-B.y); }
    19 
    20 Vector operator * (const Vector& A, double p)
    21 { return Vector(A.x*p, A.y*p); }
    22 
    23 double Cross(const Vector& A, const Vector& B)
    24 { return A.x * B.y - A.y * B.x; }
    25 
    26 double PolygonArea(const Polygon& p)
    27 {//求多边形面积
    28      int n = p.size();
    29      double area = 0;
    30      for(int i = 1; i < n-1; i++)
    31         area += Cross(p[i]-p[0], p[i+1]-p[0]);
    32     return area/2;
    33 }
    34 
    35 Point Intersection(Point P, Vector v, Point Q, Vector w)
    36 {//求两直线交点
    37     Vector u = P-Q;
    38     double t = Cross(w, u) / Cross(v, w);
    39     return P+v*t;
    40 }
    41 
    42 const double sqrt2 = sqrt(2.0);
    43 const Vector v1(1, 0), v2(0, 1), v3(1, 1);
    44 double t1, t2, s1, s2, w;
    45 Polygon poly;
    46 
    47 bool in(const Point& p)
    48 { return p.x >= t1 && p.x <= t2 && p.y >= s1 && p.y <= s2; }
    49 
    50 void check(Point p)
    51 {
    52     if(in(p) && fabs(p.x-p.y) <= w)//该点要在矩形内两平行线之间
    53         poly.push_back(Point(p.x, p.y));
    54 }
    55 
    56 int main()
    57 {
    58     //freopen("in.txt", "r", stdin);
    59 
    60     int T;
    61     scanf("%d", &T);
    62     for(int kase = 1; kase <= T; kase++)
    63     {
    64         poly.clear();
    65         scanf("%lf%lf%lf%lf%lf", &t1, &t2, &s1, &s2, &w);
    66         Point p(t1, s2), p1(0, w), p2(0, -w);
    67         check(p);
    68         p = Intersection(Point(t1, 0), v2, p1, v3); check(p);
    69         p = Intersection(Point(t1, 0), v2, p2, v3); check(p);
    70         p = Point(t1, s1); check(p);
    71         p = Intersection(Point(0, s1), v1, p1, v3); check(p);
    72         p = Intersection(Point(0, s1), v1, p2, v3); check(p);
    73         p = Point(t2, s1); check(p);
    74         p = Intersection(Point(t2, 0), v2, p2, v3); check(p);
    75         p = Intersection(Point(t2, 0), v2, p1, v3); check(p);
    76         p = Point(t2, s2); check(p);
    77         p = Intersection(Point(0, s2), v1, p2, v3); check(p);
    78         p = Intersection(Point(0, s2), v1, p1, v3); check(p);
    79         //for(int i = 0; i < poly.size(); i++) printf("%.3f %.3f
    ", poly[i].x, poly[i].y);
    80         double A = PolygonArea(poly);
    81         double B = (t2-t1) * (s2-s1);
    82         double ans = A / B;
    83         printf("Case #%d: %.8f
    ", kase, ans);
    84     }
    85 
    86     return 0;
    87 }
    代码君
  • 相关阅读:
    vscode中使用less写css样式出现红色波浪线
    vue报错error Trailing spaces not allowed no-trailing-spaces
    visual code 报错error Expected space or tab after '//' in comment spaced-comment
    笔记本电脑已经启动却黑屏
    连接MySQL报错The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone.
    IDEA搭建基于maven的springboot工程
    eclipse 下修改Dynamic Web Modulle 的问题
    firefox(火狐)下 js中设置checkbox属性checked="checked"已有,但复选框却不显示勾选的原因
    unbtun python tab补全
    python之路:进阶篇 内置函数
  • 原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/4328456.html
Copyright © 2020-2023  润新知