Sumitomo Mitsui Trust Bank Programming Contest 2019 Task F. Interval Running

Link.

There is a nice approach to this problem that involves some physical insight.
In the following we'll refer to Takahashi as A and to Aoki as B.

Without loss of generality, assume \(A_1 > B_1\). Consider A's relative motion with respect to B, i.e. imagine B is always at rest. The relative velocity of A is \(A_1 - B_1\) for the first \(T_1\) minutes and \(A_2 - B_2\) for the subsequent \(T_2\) minutes.

Let \(S = (A_1 - B_1) T_1 + (A_2 - B_2) T_2\), we have

  1. if \(S > 0\), A and B never meet,
  2. if \(S = 0\), they meet infinitely many times,
  3. if \(S < 0\), they meet some finite number of times.

In case \(S < 0\), it's not hard to figure out the number of times A and B meet.

code
 int main() {
    ll t1, t2;
    ll a1, a2, b1, b2;
    scan(t1, t2, a1, a2, b1, b2);
b1 -= a1;
b2 -= a2;
if (b1 < 0) {
    b1 = -b1;
    b2 = -b2;
}
ll s = b1 * t1 + b2 * t2;
if (s == 0) {
    println("infinity");
} else  if (s > 0) {
    println(0);
} else {
    s = -s;
    ll k = b1 * t1 / s;
    ll ans = 2 * k;
    if (b1 * t1 % s == 0) {
        ++ans;
    } else {
        ans += 2;
    }
    println(ans - 1); // -1 because we do not count the start of the run as a meet
}
return 0;

}

posted @ 2019-12-01 23:13  Pat  阅读(206)  评论(0编辑  收藏  举报