Meeting

题目描述

Two of the three members of the winning team of one of the ACM regional contests are going to meet in order to train for the upcoming World Finals. They decided that they will meet sometime between $X$ o’clock and $Y$ o’clock. Because they never get anywhere on time (they were late even on the day of the regional contest), they did not set an exact time when they will meet. However, they decided that the one who gets first at the meeting point will not wait more than $Z$ minutes for the other one (they calculated that, if the other one will not come within $Z$ minutes from the arrival of the first of them, then it is very probable that he will not show up at all).
Knowing that, in the end, both of them will show up at some time between $X$ o’clock and $Y$ o’clock (not necessarily after an integer number of minutes), compute which is the probability that they will actually meet.

题意概述

给定整数$X, Y$和实数$Z$,在$X$时到$Y$时之间任选两个时刻,求时刻之差不超过$Z$分钟的概率。
数据范围:$0 \le X \lt Y \le 24, \; 0 \lt Z \le 60(Y-X)$。

算法分析

在平面直角坐标系内以第一个时刻为$x$轴、第二个时刻满足要求的概率为$y$轴画出一个封闭图形,那么答案就是这个图形的面积除以$X$时与$Y$时之差。分两种情况讨论:$2Z \le 60(Y-X)$和$2Z \gt 60(Y-X)$。两种情况得到的封闭图形均为一个${Z \over 60(Y-X)} \times 60(Y-X)$的矩形加上一个下底长$60(Y-X)$的梯形(上底可为$0$)。前者上底加下底为$2(60(Y-X)-Z)$,高为${Z \over 60(Y-X)}$;后者上底加下底为$2Z$,高为$(1-{Z \over 60(Y-X)})$。

代码

#include <cstdio>

using namespace std;

struct Solver {
private:
  int x, y; double z;
  void input() { scanf(" %d %d %lf", &x, &y, &z); }
  void process() {
    int all = (y - x) * 60;
    double sum = 0;
    if (2 * z <= all) sum = z + 2 * (all - z) * (z / all) / 2;
    else sum = z + 2 * z * (1 - z / all) / 2;
    printf("%.8lf\n", sum / all);
  }

public:
  void solve() { input(), process(); }
} solver;

int main() {
  solver.solve();
  return 0;
}

RegMs If

418 I'm a teapot

Leave a Reply

Your email address will not be published. Required fields are marked *